Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting File to String

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?

like image 582
Abraham Andujo Avatar asked Dec 28 '22 03:12

Abraham Andujo


2 Answers

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.

like image 101
Dan Avatar answered Dec 30 '22 10:12

Dan


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

like image 33
Andrejs Avatar answered Dec 30 '22 11:12

Andrejs