Fri Jan 02, 2009 11:25 pm
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileManipulator {
private File file;
// constructor
public FileManipulator(File file) {
this.file = file;
}
public FileManipulator(String filePath) {
file = new File(filePath);
}
// reading method
public String ReadFromFile() {
String str = "", tempStr = "";
BufferedReader br;
try {
br = new BufferedReader(new FileReader(this.file));
while ((tempStr = br.readLine()) != null)
str += tempStr + "\n";
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
//writing method
public void WriteInFile(String content) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileManipulator fm = new FileManipulator("input.txt");
String content = fm.ReadFromFile();
System.out.println(content);
fm.file = new File("output.txt");
fm.WriteInFile(content);
}
}
Sat Jan 03, 2009 12:13 am
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.