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.

Results 1 to 4 of 4

Thread: java program to copy a text file to onother text file

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java program to copy a text file to onother text file

    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


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: java program to copy a text file to onother text file

    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());

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java program to copy a text file to onother text file

    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.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: java program to copy a text file to onother text file

    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.

Similar Threads

  1. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 01:40 AM
  2. write text to a file help
    By wolfgar in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 24th, 2009, 08:36 AM
  3. Creating variables from a text file
    By Larz0rz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 4th, 2009, 11:57 PM
  4. Java program to reduce spaces between the words in a text file
    By tyolu in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:17 AM
  5. [SOLVED] Enhancement in program of removing whitespace from text file
    By John in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: April 27th, 2009, 09:36 AM