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 15 of 15

Thread: Copying from one file(source) to another file(destination)

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Unhappy Copying from one file(source) to another file(destination)

    Hello, please, I have a file(source.txt) that contains some texts. Now, the java code is to run through each line in the source file and check if that line begins with a number and if true, copy that line to the destination file(destination.txt). I have the code but I am not sure why I can't loop through each line in the source.txt file. Below is my java code:

     
    public static void copyFile(File source, File destination) {
     
    		BufferedReader br;
    		try {
    			FileWriter fw = new FileWriter(destination.getAbsoluteFile());
    			PrintWriter bw = new PrintWriter(fw);
    			br = new BufferedReader(new FileReader(source));
    			String line = new String();
    			while ((line = br.readLine()) != null) {
    				String[] lineParts = line.split("\\s+");
    				   if(lineParts[0].matches("\\d+")){
     
    						bw.printf("%s" + "%n", line);
    				   }
    			}
    			br.close();
    			bw.close();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
     
    	}

    As shown in the source.txt file, there are 4 lines in the file but when the execution gets to
    while ((line = br.readLine()) != null) , it executes to false.. Anyone knows why this is so?

    below is the source.txt file

    12 is a number
    Green is bad
    5 is not so cool
    you are right


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    734
    Thanks
    0
    Thanked 133 Times in 116 Posts

    Default Re: Copying from one file(source) to another file(destination)

    Maybe this method here might help you:
    Files.readAllLines(path)
    It will read a file and return a List<String> of all lines.

    Maybe it will make your life a little easier.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,062
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Copying from one file(source) to another file(destination)

    while ((line = br.readLine()) != null) , it executes to false..
    It should be false after the lines are read.
    Have you tried debugging the code by adding a call to println() inside the loop to print out the value of line each time it is assigned a value?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Copying from one file(source) to another file(destination)

    But why is it false? the source.txt has 4 lines. So, the loop should run atleast 4 times.

    --- Update ---

    I will try this but why is the readline not true?

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,771
    My Mood
    Mellow
    Thanks
    234
    Thanked 715 Times in 701 Posts

    Default Re: Copying from one file(source) to another file(destination)

    Your method works fine just as it is in my environment. I suspect your input file is not being found.

    As Norm suggested, add some print statements to the loop to figure out what's going on.

  6. #6
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Copying from one file(source) to another file(destination)

    Hello, the problem is a bit strange as there are no errors. The only logical thing is in the method, this line is a suspect:

    br = new BufferedReader(new FileReader(source));

    because when I added println() round the br.readLine() it returned null which shouldn't be. How can I be sure it is really reading this source.txt file?

    Below is my main function
     
    public static void main(String[] args) {	
    		//the source file
    		File source = new File("source.txt");
    		System.out.println(source.getAbsoluteFile());
     
    		if (!source.exists()) {
    			try {
    				source.createNewFile();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
     
     
    		//the destination file
    		File destination = new File("destination.txt");
    		if (!destination.exists()) {
    			try {
    				destination.createNewFile();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
     
     
     
    		copyFile(source, destination);
    	}

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,062
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Copying from one file(source) to another file(destination)

    The code in post#1 works for me. Are there any lines in source.txt?
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    help_desk (July 7th, 2014)

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,771
    My Mood
    Mellow
    Thanks
    234
    Thanked 715 Times in 701 Posts

    Default Re: Copying from one file(source) to another file(destination)

    This is a good example of a try/catch that isn't doing you much good. I suspect the source file, "source.txt", isn't being found in the main() method so null is being passed to the method as the source file. Your if() statement above may not be helping at all or at best is creating an empty file, but I haven't completely worked through what's happening there.
    How can I be sure it is really reading this source.txt file?
    Ensure the File "source" is being created and pointing to the right resource. This may take some experimentation in your environment to determine how to specify the path to the desired file. For example, in my environment, using Eclipse on Linux, I have a folder named "files" at the same level as my /src directory. When I test a simple program that requires a text file, I put the text file in the folder "files" and specify the path to the file in the program as "./files/filename.txt". I don't know what will work in your environment, but you can figure it out.

  10. The Following User Says Thank You to GregBrannon For This Useful Post:

    help_desk (July 7th, 2014)

  11. #9
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Copying from one file(source) to another file(destination)

    Hi, ok I will try that . Please, this is just a side note. Imagine I have an array of integers with the following numbers {1,2,3,4} but I want to replace the 4 with ? so that in the end, the array looks something like this : {1,2,3,?}. Is this achievable? I just want to know if this can be done?

    Thanks

  12. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,771
    My Mood
    Mellow
    Thanks
    234
    Thanked 715 Times in 701 Posts

    Default Re: Copying from one file(source) to another file(destination)

    All elements of an array must be of the same type.

  13. #11
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Copying from one file(source) to another file(destination)

    So, is there no possible way to I could replace an integer value in an integer array to some character like "?" ? I have an array with the values {1,3,2,4} but want it to be {1,3,2,?} . Is this not possible by any means in Java?

  14. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,062
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Copying from one file(source) to another file(destination)

    As Greg said: All elements of an array must be of the same type.

    Why do you want a value in an int array to be a "?"?

    You could chose one of the possible int values to represent a "?" and use that.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #13
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Copying from one file(source) to another file(destination)

    You could chose one of the possible int values to represent a "?" and use that.
    But how could I do this such that I could represent the 4 in the array with "?" and still use it in the integer array? I don't see this being logically right but I am asking to be sure that I am right this can't be done. The goal I am trying to achieve is to pass an array repeatedly into some method(acting like a machine). So, say you feed it with 5 integers{1,2,3,4,5} , it should replace the last number with a "?" so, after passing it through the machine for the first time, one gets this {1,2,3,4,?} now on a second pass, one gets this {1,2,3,?,?} and on a third pass {1,2,?,?,?}..and so on..until one has the whole array filled with "?" {?,?,?,?,?}. This is what I am trying to achieve but I noticed replacing an integer with "?" inside an array is not possible. So, could I be wrong?

  16. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,062
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Copying from one file(source) to another file(destination)

    replacing an integer with "?" inside an array is not possible
    That's correct. As a work around, chose an int value to represent the "?".

    For example:
    final static int QuestionMark = 54321; // this value represents a "?"

    theArray[i] = QuestionMark; // set element at i to represent a "?"
    If you don't understand my answer, don't ignore it, ask a question.

  17. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,771
    My Mood
    Mellow
    Thanks
    234
    Thanked 715 Times in 701 Posts

    Default Re: Copying from one file(source) to another file(destination)

    There's always a way, and the way won't always (usually isn't) the first one you think of.

    If the machine you describe is mostly for show, then it could return a String representation of the desired int array: "{1,2,3,4,?}", "{1,2,3,?,?}", and "{1,2,?,?,?}". If there's some function required from those values, then you may have to parse the String representations into the necessary values. You could also use an Object[] array that could contain both Integer and String objects, but that could get messy, depending on what you do next with it.

    How the data is stored in your program doesn't have to be what you show the user.

Similar Threads

  1. [SOLVED] Putting images in GUI. Images in source file dont seem to be recognised.
    By nikolaiL in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 8th, 2014, 02:44 PM
  2. [SOLVED] Copying a file to an array and getting exception thrown for input
    By KristopherP in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 1st, 2013, 12:07 AM
  3. Copying a File
    By ubiByte in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 19th, 2012, 06:55 PM
  4. Calling a nested class from a different java source file?
    By Pyrius in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 19th, 2011, 07:16 PM
  5. Replies: 6
    Last Post: May 25th, 2010, 02:15 AM

Tags for this Thread