【问题标题】:Converting File to String将文件转换为字符串
【发布时间】:2012-04-17 10:21:19
【问题描述】:

如何在新类文件的代码中将文件转换为字符串?我现在很迷茫,我试图将文件转换为字符串,但是,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 转换为新类文件中的字符串?

【问题讨论】:

标签: java string file class


【解决方案1】:

看看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。

【讨论】:

  • 我还是不明白怎么做。你能发布一些示例代码吗?我不知道如何整合它。
【解决方案2】:

您可以使用来自 Google 库的 Files.toStringFiles.readLines 方法

【讨论】:

  • 但是文件 f = new File(Mover.src);但这不起作用,我试图让 src 成为一个字符串。您能否发布一些示例代码以便我可以使用?
  • 如果您想要文件的内容,那么这是正确的做法。否则......我真的不知道你想做什么。
【解决方案3】:

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

或者,在Apache Commons IO 中使用FileUtils.readFileToString(File file) 怎么样?在那里您还可以找到FileUtils.copyFile(File srcFile, File destFile) 之类的方法。

【讨论】:

  • 你能发布一些示例代码吗?我还是 java 新手 :(.
  • 就像我说的,你能发布一些示例代码吗?除非我有事可做,否则我不会得到它!
  • 那不是我要找的......我正在寻找如何将文件 src 转换为字符串,但在一个新类中。如您所见,我已经这样做了。
猜你喜欢
  • 1970-01-01
  • 2011-01-06
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
  • 2012-03-28
  • 1970-01-01
相关资源
最近更新 更多