4

How can I convert file to string in my code in a new class file? I'm so lost right now, im trying to convert a file to string, but, eclipse keeps saying that it does not exist, and that it won't work. here is my code.

package Mover;
import java.io.*;

 public class Mover {


    public static void main(String[] args) throws IOException, InterruptedException {   

        String desktop = FindDesktop.getCurrentUserDesktopPath();
        String usb = new File(".").getAbsolutePath();
        File TS3S = new File(usb + "/Teamspeak 3");
        File TS3D = new File(desktop + "/TS3");
        File MinecraftLauncherS = new File(usb + "/Minecraft");
        File MinecraftLauncherD = new File(desktop);
        File ShortcutS = new File(usb + "/Shortcuts");
        File ShortcutD = new File(desktop);
        File FrapsS = new File(usb + "/Fraps");
        File FrapsD = new File(desktop + "/Fraps");

        //make sure source exists
        if(!TS3S.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(TS3S,TS3D);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        //make sure source exists
        if(!MinecraftLauncherS.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(MinecraftLauncherS,MinecraftLauncherD);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        //make sure source exists
        if(!ShortcutS.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(ShortcutS,ShortcutD);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        //make sure source exists
        if(!MinecraftLauncherS.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(FrapsS,FrapsD);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        System.out.println("Done");
        Runtime.getRuntime ().exec (desktop + "/TS3/ts3client_win32.exe");
        Runtime.getRuntime ().exec (desktop + "/Minecraft.jar");
        Runtime.getRuntime ().exec (desktop + "/Fraps/fraps.exe");
        System.exit(0);
        }

    public static void copyFolder(File src, File dest)
        throws IOException{

        if(src.isDirectory()){

            //if directory not exists, create it
            if(!dest.exists()){
               dest.mkdir();
               System.out.println("Directory copied from " 
                              + src + "  to " + dest);
            }

            //list all the directory contents
            String files[] = src.list();

            for (String file : files) {
               //construct the src and dest file structure
               File srcFile = new File(src, file);
               File destFile = new File(dest, file);
               //recursive copy
               copyFolder(srcFile,destFile);
            }

        }else{
            //if file, then copy it
            //Use bytes stream to support all file types
            InputStream in = new FileInputStream(src);
                OutputStream out = new FileOutputStream(dest); 

                byte[] buffer = new byte[1024];

                int length;
                //copy the file content in bytes 
                while ((length = in.read(buffer)) > 0){
                   out.write(buffer, 0, length);



                in.close();
                out.close();
                System.out.println("File copied from " + src + " to " + dest);
        }
    }
    }
 }

See, I dont know how it cant print out the file name src (its prints out a directory adress on a disk). I want to convert that data into a string so i can place it into a JFrame. But the thing is, I cant find any code out there that I can get to work, i dont know if the code itself doesn't work, or I'm just doing it wrong. So what code could I make to convert src to string in a new class file?

Abraham Andujo
  • 147
  • 1
  • 6
  • 12

3 Answers3

8

Take a look at Apache Commons FileUtils and its readFileToString(File source) or readFileToString(File source, String encoding) methods. Here's a sample:

public static void main(String[] args){
    File myFile = new File("/home/user/readme.txt");

    try{
        FileUtils.readFileToString(myFile);
    }catch(IOException e){
        e.printStackTrace();
    }   
}

You just need to make sure to include the commons-io jar in your project's classpath.

Dan
  • 1,763
  • 4
  • 26
  • 40
2

You could use Files.toString or Files.readLines method from Google libraries

Andrejs
  • 26,885
  • 12
  • 107
  • 96
  • but what about File f = new File(Mover.src); that doesnt work though, im trying to get src to become a string. Could you maybe post some example code so i can work with? – Abraham Andujo Apr 03 '12 at 18:08
  • If you want the _contents_ of a file, then this is the right thing to do. Otherwise...I really don't know what you're trying to do. – Louis Wasserman Apr 03 '12 at 19:27
0

Are you using Java 7? Take a look at the new Files.java with methods like copy(Path source, Path target, CopyOption... options)

Alternatively, how about using FileUtils.readFileToString(File file) in Apache Commons IO? There you will also find methods like FileUtils.copyFile(File srcFile, File destFile).

matsev
  • 32,104
  • 16
  • 121
  • 156