hi to all community
I would need some help to find a java program to copy text file into a different text file.
did some of you develop a simple program to do this ????
thanks in advance
francoc
Printable View
hi to all community
I would need some help to find a java program to copy text file into a different text file.
did some of you develop a simple program to do this ????
thanks in advance
francoc
Just invoke a batch file to copy the file, or use the OS gui to copy the file for you.
If it must be in Java, use the BufferedWriter and FileWriter classes to write out to a file, and Scanner or BufferedReader/FileReader to read in
Code :BufferedWriter writer = new BufferedWriter(new FileWriter("test file.txt")); writer.append("this is some test text."); writer.flush(); writer.close;
Code :Scanner reader = new Scanner("test file.txt"); System.out.println("First line read: "); System.out.println(reader.nextLine());
check this link out Byte Streams (The Java™ Tutorials > Essential Classes > Basic I/O) it gives u and example and you can modify it to fit whatever you want.:rolleyes::-bd
Hey francoc,
Welcome to the forums :D
How about this?
Code :import java.io.*; import java.util.Scanner; public class FileProgram { public static void main(String[] args) throws Exception { readFile(); } public static void readFile() throws Exception { // Location of file to read File file = new File("data.txt"); Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); //System.out.println(line); writeFile(line); } scanner.close(); System.out.println("File Copied"); } public static void writeFile(String copyText) throws Exception { String newLine = System.getProperty("line.separator"); // Location of file to output Writer output = null; File file = new File("date_output.txt"); output = new BufferedWriter(new FileWriter(file, true)); output.write(copyText); output.write(newLine); output.close(); } }