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
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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
BufferedWriter writer = new BufferedWriter(new FileWriter("test file.txt")); writer.append("this is some test text."); writer.flush(); writer.close;
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.
Hey francoc,
Welcome to the forums
How about this?
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(); } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.