---
title: "Classen file"
type: "forum-thread"
url: "https://www.webforum.nu/amne/java/76843-classen-file"
topic: "Java"
topic_url: "https://www.webforum.nu/amne/java"
author: "AG"
published: "2003-05-13T21:11:40.000Z"
updated: "2003-05-15T17:40:27.000Z"
replies: 4
views: 333
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/java/76843-classen-file"
---

# Classen file

## #1 — AG, 2003-05-13T21:11Z

Följande kod använder jag för att hitta och kopiera en fil till en speciell katalog.

```
<%@ page import="java.io.*" %>
<%
// File (or directory) to be moved
File file = new File("c:/dir/banner2.bmp");   
// Destination directory
File dir = new File("c:/dir/banner");    
// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));
if (!success) {
System.out.println("File was not successfully moved");  
// File was not successfully moved
}
else{
System.out.println("File was successfully moved");
}
%>
```

Dock skulle jag vilja slippa, eller rättare sagt, måste slippa att behöva ange en hel sökväg, (c:/...).
Jag vill att programmet skall utgå från den katalogen programmet ligger i. Dvs banner2.bmp ligger i samma katalog som denna jspsida, och katalogen banner är en katalog i denna katalog.

Tack
Andreas

Permalänk: https://www.webforum.nu/p/76843

## #2 — Viktor, 2003-05-13T21:31Z

Du kan använda "pageContext.getServletContext().getRealPath("")" för att få sökvägen till din JSP sida.

```
<%@ page import="java.io.*" %>
<%
// File (or directory) to be moved
File file = new File(pageContext.getServletContext().getRealPath("")+"/space.gif");   
// Destination directory
File dir = new File(pageContext.getServletContext().getRealPath("")+"/jffc/");    
// Move file to new directory
if(!file.renameTo(new File(dir, file.getName())))
	out.println("File was not successfully moved");  
else
	out.println("File was successfully moved");
%>
```

Flyttar tråden till J2EE.

/Viktor

Permalänk: https://www.webforum.nu/p/1048392

## #3 — AG, 2003-05-14T16:37Z

Upptäckte att detta flyttade hela filen. Skulle endast vilja att den kopierade filen, och byta namn på den i den nya katalogen.

Tack
Ändreas

Permalänk: https://www.webforum.nu/p/1049164

## #4 — Viktor, 2003-05-14T19:44Z

Då får du skapa den nya filen och sedan kopiera över innehållet i den gammla filen till den nya.

```
<%@ page import="java.io.*" %>
<%
FileInputStream fis  = new FileInputStream(new File(pageContext.getServletContext().getRealPath("")+"/space.gif"));
FileOutputStream fos = new FileOutputStream(new File(pageContext.getServletContext().getRealPath("")+"/jffc/MY_NEW_NAME.gif"));
byte[] buf = new byte[1024];
int i = 0;
while((i=fis.read(buf))!=-1)
{
   fos.write(buf, 0, i);
}
fis.close();
fos.close();
%>
```

Koden är testad och fungerar :)

/Viktor

Permalänk: https://www.webforum.nu/p/1049398

## #5 — AG, 2003-05-15T17:40Z

Tack, men jag hittade en annan lösning, fungerar som nedan.

```
'copyFile.java
import java.io.*;
import java.util.*;

public class copyFile {

	public boolean copyFile(String source,String target) throws IOException
	{
	   File filesource=new File(source);
	   if(filesource!=null)
	   {
	      if(filesource.exists()&&filesource.isFile())
	      {
	         File filetarget=new File(target);
	         if(filetarget!=null)
	         {
            
	            filetarget.createNewFile();   
	            if(filetarget.exists()&&filetarget.isFile())
	            {
         
	               byte [] data;
	               FileInputStream fis = new FileInputStream(filesource);
	               FileOutputStream fos=new FileOutputStream(filetarget);

	               data=new byte[fis.available()];
	               while(fis.read(data)>0)
	                { 
             
	                fos.write(data);
	                data=new byte[fis.available()];
	             }
             
	               fis.close();
	               fos.close();
	            }
	            else throw new IOException(target+" is not a file");      
	         }
	         else throw new IOException("Can't instatiate class File with String parameter "+target);            

         
	      }
	      else throw new IOException(source+" is not a file");      
	   }
	   else throw new IOException("Can't instatiate class File with String parameter "+source);      
	   return(true);   
      
	}

}

'jspsidan
<%@ page import="java.io.*" %>
<%@ page import="java.net.*" %>
<%@ page import="copyFile" %>
<%

// File (or directory) to be moved
String source = pageContext.getServletContext().getRealPath("")+"img/banner2.bmp";
String target = pageContext.getServletContext().getRealPath("")+"pub/banner.bmp";    
// Move file to new directory
copyFile cf = new copyFile();

if(!cf.copyFile(source,target)){
	System.out.println("Failed in creation of banner");
	out.println("Failed in creation of banner");
}
else{
	System.out.println("Banner uploaded");
	out.println("Banner uploaded");
}
%>
```

Andreas

Permalänk: https://www.webforum.nu/p/1050182

---

Tråden på webben: https://www.webforum.nu/amne/java/76843-classen-file
