
-----------------------------------
Shivu
Sat Jun 14, 2008 12:12 am

File Reader!- Help!!
-----------------------------------
HI

i have some problems understanding file reader and biffer reader, meaning how does the thing read the line (or does it read each character separately- i don't even know this) does it read it as a char, a s tring an int...

(since i cannot post my entire problem at oce ( i dunno why, an error comes up), i'm going to post them one after the other... sry bout that)

-----------------------------------
Shivu
Sat Jun 14, 2008 12:12 am

Re: File Reader!- Help!!
-----------------------------------
so i have to answer a question regarding this topic and i don't know how to implement it.

If one could please help me in this regard, that'd be great :)

here is the question:
In the text file ?data.txt?, the first line contains an integer which indicates the number of lines that follow.  You program should read each one of these lines and then output on the screen the one with the longest length.

-----------------------------------
Shivu
Sat Jun 14, 2008 12:13 am

Re: File Reader!- Help!!
-----------------------------------
my code:




   import java.io.*; 

    public class Test {
   
       public static void main (String [] args){
      
         FileReader file;	
         BufferedReader buffer;
         String line; 
         int line2;     
         String temp;  
      	   
         try { 
         
            file = new FileReader ("data.txt");
            buffer = new BufferedReader (file);
         	
            line2 = buffer.read();
         	
            temp = buffer.readLine();
            				
         
            for (int i =0; i temp.length()){
               
                  temp = line;
               }
            
            }
         	
              	System.out.println (temp);
         	
         
         }         
             catch ( IOException iox ) { 
               System.out.println("Problem reading data.txt"); 
            } 
      
      }
   }



-----------------------------------
Shivu
Sat Jun 14, 2008 12:14 am

Re: File Reader!- Help!!
-----------------------------------
My code is wrong because whenever i try to run it an exception comes up:  nullpointerexception...

so, i don't know how to rectify it....

and if you can explain the logic / concept, that'd be great (or explain what you are trying to say :))

thank  you!


here's the "data.txt" file... the input can be anything.. but here's something to work with!

3
Hello^World
ICS
HelloWorld

-----------------------------------
Euphoracle
Sat Jun 14, 2008 12:16 am

Re: File Reader!- Help!!
-----------------------------------
so i have to answer a question regarding this topic and i don't know how to implement it.

If one could please help me in this regard, that'd be great :)

here is the question:
In the text file ?data.txt?, the first line contains an integer which indicates the number of lines that follow.  You program should read each one of these lines and then output on the screen the one with the longest length.


...
            file = new FileReader ("data.txt");
            buffer = new BufferedReader (file);
            
            line2 = buffer.read();  right? because if i read it as an int initially, only the ascii value will be stored... and i can't change that ascii to the int value?

thanks!

-----------------------------------
Euphoracle
Sat Jun 14, 2008 1:04 am

RE:File Reader!- Help!!
-----------------------------------
It does not include the line break characters.  It skips over those and simply returns the value of the line as you would see it in Notepad, for example.

The buffer.read() method returns the ascii value (assumed ascii encoded file) of the next character.  So if you called it first, it would report 51, the ascii value of "3".

-----------------------------------
Shivu
Sat Jun 14, 2008 1:06 am

RE:File Reader!- Help!!
-----------------------------------
if it does not read the break characters, how does it go to the next line?...

but the break characters are always present ?

i ask too many questions... sry 

thanks!!

-----------------------------------
Euphoracle
Sat Jun 14, 2008 1:24 am

RE:File Reader!- Help!!
-----------------------------------
- It skips the break characters, but still situates the position after the break characters.

- Without break characters, there's no line break.  The exception is the EOF, which is read as a single line from the last break character.

-----------------------------------
Zeroth
Sat Jun 14, 2008 8:42 am

Re: File Reader!- Help!!
-----------------------------------
It does NOT include the \r\n. (Note the order of the two characters. That is important.) Then it moves onto the next line. 

Here's an example that might help:
say you're building a telnet application. In telnet, all enters are translated into \r\n. 
Treat the incoming connections like just a file, and use Bufferedreader on them. It will keep trying to read from the connection until it gets the pair \r\n, then it returns all the data its grabbed.

Now, to turn an ascii into to a char... have you been taught about casting yet?

Here's the solution:

int i = 65;
char ch = (char)i;
ch='A'


I want you to look at that, and think about what is happening. Its taking the int value... and what? is the char value any different? Or, maybe its the context that is different(for the program)? The idea here is that you will understand more than just solving this one problem, but look at what you've learned, and be able to apply to harder ones.
