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

Thread: Java IO File Reader Question???

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java IO File Reader Question???

    Hi I was given this class and method and asked to:

    " Please list your observations about this class, including bugs, poor programming, inefficiencies, etc..."

    And wrote an alternative version using a Scanner instead and identified the forward slashes should be back slashes and escaped and provided my own readFileFixed() method, however I was told that this wasn't a very good solution and that I should go off and think a bit more about it, but cant really think of anything else and would be really greatful for any hints, tips or Advice....

    package codeTest;
     
    import java.io.File;
    import java.io.FileReader;
     
     
    public class CodeTestExerciseBadCode {
     
    	public CodeTestExerciseBadCode(){
     
    	}
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		CodeTestExerciseBadCode part2 = new CodeTestExerciseBadCode();
    		System.out.println(part2.readFile());		
    	}
     
    	public String readFile(){
    		File f = null;
    		FileReader fr = null;
    		StringBuffer content = null;
    		try{
    			f = new File("c:/samplefile.txt");
    			fr = new FileReader(f);
     
    			int c;			
    			while((c = fr.read()) != -1){				
    				if(content == null){
    					content = new StringBuffer();
    				}
     
    				content.append((char)c);
    			}
     
    			fr.close();			
    		}
    		catch (Exception e) {
    			throw new RuntimeException("An error occured reading your file");
    		}		
     
    		return content.toString();
    	}
    }


    And my solution
    public String readFileFixed() {
     
    		StringBuffer content;
    		content = new StringBuffer();
     
    		File file = new File("C:\\here\\sampletextfile.txt");
    		try {
    			Scanner scanner = new Scanner(file);
    			while (scanner.hasNextLine()) {
    				content.append(scanner.nextLine());
    				content.append(System.getProperty("line.separator"));
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
     
    		return content.toString();
    	}

    So any suggestions would be great, thanks...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,011
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Java IO File Reader Question???

    I'd move
    System.getProperty("line.separator")
    outside of the loop and have it store the value in a local variable instead of calling the method each time around the loop.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java IO File Reader Question???

    Hi thanks for that!!!

Similar Threads

  1. Java File IO question :confused:
    By byebyebye in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: August 17th, 2010, 06:45 AM
  2. Problems with File Reader (Strings and 2D Array Storage)
    By K0209 in forum File I/O & Other I/O Streams
    Replies: 44
    Last Post: January 12th, 2010, 09:48 AM
  3. Buffered Reader is not reading my file properly... HELP!
    By mannyT in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 8th, 2009, 07:14 PM
  4. greetings and a file reader problem
    By chileshe in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: October 6th, 2009, 03:45 AM
  5. Code to read a character in the file
    By Truffy in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 19th, 2009, 06:11 PM

Tags for this Thread