将文件转换为字符串

时间:2012-04-03 17:59:50

标签: java string file class

如何在新的类文件中将文件转换为我的代码中的字符串?我现在迷失了,我试图将文件转换为字符串,但是,eclipse一直说它不存在,并且它不会起作用。这是我的代码。

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);
        }
    }
    }
 }

看,我不知道它怎么打印出文件名src(它打印出磁盘上的目录地址)。我想将该数据转换为字符串,以便将其放入JFrame中。但问题是,我无法找到任何可以开始工作的代码,我不知道代码本身是否无效,或者我只是做错了。那么我可以将src转换为新类文件中的字符串?

3 个答案:

答案 0 :(得分:7)

查看Apache Commons FileUtils及其 readFileToString (文件来源)或 readFileToString (文件来源,字符串编码)方法。这是一个示例:

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

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

您只需要确保在项目的类路径中包含commons-io jar。

答案 1 :(得分:2)

您可以使用Google Files.toString图书馆的Files.readLines方法

答案 2 :(得分:0)

您使用的是Java 7吗?使用类似的方法查看新的Files.java copy(Path source, Path target, CopyOption... options)

或者,如何在FileUtils.readFileToString(File file)中使用Apache Commons IO?在那里,您还可以找到FileUtils.copyFile(File srcFile, File destFile)等方法。