Convert file to string
How do I convert a file to a string in my code to a new class file? I am so lost right now, I am trying to convert the file to a string, but Eclipse keeps saying that it is not there and that it will not 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 don't know how it can't print the src filename (it prints the address of a directory on disk). I want to convert this data to a string so that I can put it in a JFrame. But the thing is, I can't find any code that I can get, I don't know if the code itself isn't working, or I'm just doing it wrong. So what code can I do to convert src to string in a new class file?
source to share
Have a look at Apache Commons FileUtils and its readFileToString (file source) or readFileToString (file source, String encoding). 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 include the combo io jar in your project path.
source to share
Are you using Java 7? Take a look at the new Files.java with methods like copy (path source, path target, CopyOption ...
Alternatively, how about using FileUtils.readFileToString in Apache Commons IO ? There you will also find methods such as FileUtils.copyFile (file srcFile, File destFile) .
source to share