Converting File to String

20,838

Solution 1

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.

Solution 2

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

Share:
20,838
Abraham Andujo
Author by

Abraham Andujo

Updated on July 05, 2022

Comments

  • Abraham Andujo
    Abraham Andujo almost 2 years

    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
    Abraham Andujo about 12 years
    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
    Abraham Andujo about 12 years
    can you please post some example code? Im still new at java :(.
  • Abraham Andujo
    Abraham Andujo about 12 years
    I still dont understand how. Can you please post some example code? I dont know how to intergrate it.
  • Abraham Andujo
    Abraham Andujo about 12 years
    like i said, can you PLEASE post some example code? I wont get it until i have something to go off of!
  • Abraham Andujo
    Abraham Andujo about 12 years
    Thats not what I'm looking for.... Im looking for how to convert file src to string, but inside of a new class. as you can see, i already did that.
  • Louis Wasserman
    Louis Wasserman about 12 years
    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.