Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

java file access problem..

  • 12-05-2008 1:35pm
    #1
    Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭


    I'm having a problem with this code.. During compile it tells me:
    cannot find symbol variable wordsP and wordsB

    i'm not familiar with all these access declarations so I ould be here all day if I tried to solve it mysef.
    try
    {
    	words.createNewFile();
    	BufferedReader wordsB = new BufferedReader(new FileReader("words.txt"));
    	File answers = new File("answers.txt");
    	PrintWriter wordsP = new PrintWriter(new FileWriter("answers.txt"));
    }
    
    catch (Exception e)
    {
    	System.out.println(e.getMessage());
    }
    
    
    String s ="";
    
    while (s!=null)
    {
    	s=wordsB.readLine();
    	s=compareWords(s);
    	wordsP.println(s);
    }
    

    When I take the wordsB and wordsP declarations out of the try block I get a bunch more errors including one from s=wordsB.readLine();

    Too many errors,too many errors!!:eek:

    Thanks


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 10,088 Mod ✭✭✭✭marco_polo


    If you declare variables in the try{...} block then they are only in scope (accessible) for the duration of that try block. You will have to move the declarations out of the try block and into the main body of the method if you need to use them elsewhere within the method.
    BufferedReader wordsB;
    PrintWriter wordsP;
    try
    {
    	words.createNewFile();
    	wordsB = new BufferedReader(new FileReader("words.txt"));
    	File answers = new File("answers.txt");
    	wordsP = new PrintWriter(new FileWriter("answers.txt"));
    }
    

    Then you can start fixing the other errors ;)


  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    actually just before I got you're reply I copped that..

    I put the String s ="" and while loop in the try block, then, hey presto!!

    Thanks though!:)


  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    Did you by any chance spot another error, one being that nothing appears in answers.txt ??:( My patience is wearing thin with this!
    I've tried adding

    wordsP.close();

    after the loop..


  • Moderators, Science, Health & Environment Moderators Posts: 10,088 Mod ✭✭✭✭marco_polo


    Does an empty file get created?

    Do a System.out.println(s) just before writing to the file to verify the String isn't empty.


  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    yes, I've inserted a println() any time after an s assignment..

    Everythin appears just the way I want it to on the console but nothing in the text file..

    This one works perfect..
    try
    {
    	File answers = new File("write.txt");
    	PrintWriter wordsP = new PrintWriter(new FileWriter("write.txt"));
    		
    	String s="";
    	int i=0;
    	while (!s.equals("q"))
    	{
    		System.out.print("S:= ");s=input.nextLine();
    		wordsP.println(s);
    	}	
    	wordsP.close();
    }
    

    Everything in here seems to be in the main program i'm working with..


  • Advertisement
  • Moderators, Science, Health & Environment Moderators Posts: 10,088 Mod ✭✭✭✭marco_polo


    techguy wrote: »
    yes, I've inserted a println() any time after an s assignment..

    Everythin appears just the way I want it to on the console but nothing in the text file..

    This one works perfect..
    try
    {
    	File answers = new File("write.txt");
    	PrintWriter wordsP = new PrintWriter(new FileWriter("write.txt"));
    		
    	String s="";
    	int i=0;
    	while (!s.equals("q"))
    	{
    		System.out.print("S:= ");s=input.nextLine();
    		wordsP.println(s);
    	}	
    	wordsP.close();
    }
    

    Everything in here seems to be in the main program i'm working with..

    Try flush the stream before closing it, this should force everything that has been buffered by the Printwriter to be written out to the file.

    wordsP.flush();
    wordsP.close();


  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    Perfect!

    I inserted that at the very last line of the loop!!

    Thanks!!


  • Registered Users, Registered Users 2 Posts: 93 ✭✭Yarnhall


    Use the BufferedWriter to write the file and dont forget to close the streams if there are exceptions...
    BufferedWriter wordsP = new BufferedWriter(new FileWriter("answers.txt"));
    BufferedReader wordsB = new BufferedReader(new FileReader("words.txt"));
    String s;
    while ( (s=wordsB.readLine()) != null){ // readLine() will return null at EOF
    	s=compareWords(s);
           wordsP.write(s);
           wordsP.newLine();
    }
    wordsP.flush();
    wordsP.close();
    wordsB.close();
    


  • Registered Users, Registered Users 2 Posts: 2,236 ✭✭✭techguy


    Is there much of a difference between PrintWriter and BufferedWriter?

    Would the finally block be the place for the flush and close commands..?

    Thanks..


  • Moderators, Science, Health & Environment Moderators Posts: 10,088 Mod ✭✭✭✭marco_polo


    techguy wrote: »
    Is there much of a difference between PrintWriter and BufferedWriter?

    Would the finally block be the place for the flush and close commands..?

    Thanks..

    Not much really a PrintWriter simply provides a nice Wrapper interface for writing objects and primitives such as int to a file, whereas a BufferedWriter writes only characters. Internally a PrintWriter will use some subclass of the Writer or OutputStream classes (such as FileWriter, BufferedWriter , FileOutputStream for example) to actually write data to the file, depending on which form of the constructor you use.

    In much the same way BufferedWriter which, writes character to a file, will ultimately have to uses another class such as OutputStreamWriter internally to convert charachters into bytes to actual physically write data to the file system.

    Just the close should do in the finally block, as it is supposed to do a flush automatically when close is invoked (although this is not the behaviour you saw?). No harm to put both in there if you prefer though.


  • Advertisement
Advertisement