java 将文件转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9998759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting File to String
提问by Abraham Andujo
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.
如何在新类文件中的代码中将文件转换为字符串?我现在很迷茫,我试图将文件转换为字符串,但是,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);
}
}
}
}
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?
看,我不知道它怎么不能打印出文件名 src (它打印出磁盘上的目录地址)。我想将该数据转换为字符串,以便将其放入 JFrame 中。但问题是,我找不到任何可以开始工作的代码,我不知道代码本身是否不起作用,或者我只是做错了。那么我可以编写什么代码来将 src 转换为新类文件中的字符串?
回答by Dan
Take a look at Apache Commons FileUtilsand its readFileToString(File source) or readFileToString(File source, String encoding) methods. Here's a sample:
查看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();
}
}
You just need to make sure to include the commons-io jar in your project's classpath.
您只需要确保在项目的类路径中包含 commons-io jar。
回答by Andrejs
You could use Files.toStringor Files.readLines
method from Google guavalibraries
您可以使用Google番石榴库中的Files.toString或Files.readLines
方法
回答by matsev
Are you using Java 7? Take a look at the new Files.javawith methods like copy(Path source, Path target, CopyOption... options)
您在使用 Java 7 吗?使用copy(Path source, Path target, CopyOption... options)等方法 查看新的Files.java
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).
或者,如何在Apache Commons IO 中使用FileUtils.readFileToString(File file)?在那里你还会找到像FileUtils.copyFile(File srcFile, File destFile) 这样的方法。