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

Thread: So you want to know how to modify a file...

  1. #1
    Member Cronus's Avatar
    Join Date
    Feb 2013
    Location
    Gothenburg, Sweden
    Posts
    41
    My Mood
    Inspired
    Thanks
    6
    Thanked 7 Times in 6 Posts

    Smile So you want to know how to modify a file...

    *Thread closed*


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: So you want to know how to modify a file...

    try {
        scanner = new Scanner(new File("/Users/YourName/Desktop/originalFile.txt")); //This is a mac directory
    } catch (FileNotFoundException e) { //If the file does not exist, show a fancy output
     
        System.out.println("This file does not exist!");
        /*
         *If you want to, you can also add "e.printStackTrace();", but we already know
         *what the problem is. printStackTrace(); shows what the error is. "e" is the exceptions name.
         */
    }
    In fact a FileNotFoundException is thrown under all sorts of circumstances (access rights, invalid syntax) so we don't really now what the problem is.

    If you are going to include exception logic in the example the catch block should probably return, otherwise there will be a NullPointerException later.

    Did you mean to write the altered words to the output file? Otherwise the FileWriter isn't doing anything, and the converted words are never used.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Cronus (June 25th, 2013)

  4. #3
    Member Cronus's Avatar
    Join Date
    Feb 2013
    Location
    Gothenburg, Sweden
    Posts
    41
    My Mood
    Inspired
    Thanks
    6
    Thanked 7 Times in 6 Posts

    Default Re: So you want to know how to modify a file...

    This is just a half done code, I wrote it quickly, I'll modify it. You learn something new every day!

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: So you want to know how to modify a file...

    I hoped you wouldn't mind that I commented. (I assumed that or you wouldn't have posted it publicly.)

  6. #5
    Member Cronus's Avatar
    Join Date
    Feb 2013
    Location
    Gothenburg, Sweden
    Posts
    41
    My Mood
    Inspired
    Thanks
    6
    Thanked 7 Times in 6 Posts

    Default Re: So you want to know how to modify a file...

    I don't mind at all, I'm actually glad you optimized the code. Congrats on getting 900 posts!

Similar Threads

  1. [SOLVED] How do you modify an existing file?
    By Cronus in forum Java Theory & Questions
    Replies: 0
    Last Post: May 27th, 2013, 12:39 PM
  2. How can i modify this to work ?
    By steven_bishop in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2012, 06:45 PM
  3. read and modify a text file
    By Ayda Alhosany in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: February 25th, 2012, 11:00 PM
  4. How to modify System.out.println()
    By emailkia in forum Java Theory & Questions
    Replies: 6
    Last Post: April 25th, 2011, 12:40 AM
  5. File I/O Modify Text Problem
    By Fordy252 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 26th, 2010, 04:30 AM

Tags for this Thread