Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By allaudin
  • 1 Post By DarrylBurke

Thread: reading from a file

  1. #1
    sayswho is offline Member
    Join Date
    Jul 2013
    Location
    Dublin, CA
    Posts
    12
    Rep Power
    0

    Default reading from a file

    Hi,

    It must be something pretty obvious but I do not see it :

    Java Code:
    package practice3;
    
    import java.io.*;
    
    public class FileManipulation {
    
    	
    	String filename = "filetest.txt";
    	String filelocation = "C:/MyTest/filetest.txt"; 
    	File file =new File(filelocation);
    	
    	public boolean createFile() throws IOException
    	{
    		if (file.exists()==false)
    		{
    			
    			System.out.println("The \"filetest.txt\" does not exist yes. Successfully created new file - "
    			+ file.createNewFile());
    		}	
    		
    		else 
    		{
    			System.out.println("File " + filename +" already exists");
    		}
    		
    		return file.exists();
    	}
    	
    	public boolean writeToFile() throws IOException
    	{
    		FileWriter writer = new FileWriter(filelocation);
    		
    		System.out.println("can write to "+ filename + "? " + file.canWrite());
    		String somewords = "kkl;h";
    		writer.write(somewords);
    		writer.close();
    		System.out.println("Writing to "+ filename +"-"+ somewords);
    				return true;
    	}
    	
    	public boolean readFromFile() throws IOException
    	{
    		String content;
    		BufferedReader br = new BufferedReader (new FileReader(filename));
    		 while ((content = br.readLine()) != null)
    		  {
    			 System.out.println("Inside of "+ filename + " read: "+ content);
    		  }
    		
    		br.close();
    		
    		return false;
    	}
    	
    }
    
    class TestFileManipulation
    {
    	public static void main(String[] args) throws IOException {
    		
    		FileManipulation test = new FileManipulation();
    		test.createFile();
    		test.writeToFile();
    		test.readFromFile();
    		
    
    	}
    
    }
    Here is the output:

    The "filetest.txt" does not exist yes. Successfully created new file - true
    can write to filetest.txt? true
    Writing to filetest.txt-kkl;h
    Inside of filetest.txt read: this is some random string of words
    This line Inside of filetest.txt read: this is some random string of words is obviously wrong.
    Line 34
    Java Code:
    String somewords
    used to be "this is some random string of words" but then I replaced it with a new value.

    I can create file, I can write to it..but for some reason it keep reading the old value of String somewords.
    I tried to clean project just in case.

    Any help is welcome.

  2. #2
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: reading from a file

    Delete the contents of old file or create a new file .
    Villa likes this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: reading from a file

    There are two files in two different folders.

    db
    sayswho likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    sayswho is offline Member
    Join Date
    Jul 2013
    Location
    Dublin, CA
    Posts
    12
    Rep Power
    0

    Default Re: reading from a file

    Quote Originally Posted by DarrylBurke View Post
    There are two files in two different folders.

    db
    Thank you

Similar Threads

  1. Need help file reading a txt file (Emergency)
    By 765891 in forum New To Java
    Replies: 6
    Last Post: 02-28-2013, 12:48 AM
  2. Replies: 0
    Last Post: 12-15-2012, 01:37 AM
  3. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •