The Artima Developer Community
Sponsored Link

Java Answers Forum
Files - reading in characters

5 replies on 1 page. Most recent reply: Mar 23, 2002 12:02 PM by Bodkins

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Bodkins

Posts: 5
Nickname: bodkins
Registered: Mar, 2002

Files - reading in characters Posted: Mar 22, 2002 10:04 AM
Reply to this message Reply
Advertisement
So far i've got my program to read the first string of a file using the following:

BufferedReader in = new BufferedReader(new java.io.FileReader(filename));

String line = in.readLine();

I've tried to stick this in a loop while Line != null is the ending clause i need, but how can inplement a point to go to the next line? any help would be great thanks.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Files - reading in characters Posted: Mar 22, 2002 12:46 PM
Reply to this message Reply
Here's an example:
import java.io.*;
 
public class NumberedLines
{
    public static void main( String [] args )
    {
        if( args.length == 1 )
            DisplayFileWithLineNumbers(args[0]);
        else
            System.out.println("Specify a filename!");
    }
 
    public static void DisplayFileWithLineNumbers( String filename )
    {
        try
        {
            BufferedReader in = new BufferedReader(new java.io.FileReader(filename));
            String line = in.readLine();
            for( int i = 1; line != null; i++ )
            {
                System.out.println( i + ". " + line );
                line = in.readLine();
            }
        }
        catch( FileNotFoundException fnfe )
        {
            System.out.println("Bronk!  Can't find the file \"" + filename + "\"");
        }
        catch( IOException ioe )
        {
            System.out.println("Bronk!  Error while trying to read \"" + filename + "\"");
        }
    }
}

Bodkins

Posts: 5
Nickname: bodkins
Registered: Mar, 2002

Re: Files - reading in characters Posted: Mar 23, 2002 3:24 AM
Reply to this message Reply
Thanks i got it working!!!!

Bodkins

Posts: 5
Nickname: bodkins
Registered: Mar, 2002

Re: Files - reading in characters Posted: Mar 23, 2002 6:32 AM
Reply to this message Reply
How do either read in single characters or convert the strings im capturing in to characters, this seems to be complicated i get a lot of incompatable type errors when changing code so far i gots:

System.out.println("The file is readable, read away!");
BufferedReader in = new BufferedReader(new java.io.FileReader(filename));

line = in.readLine();

for(int i=1; line != null; i ++){
System.out.println( i + ". " + line );
line = in.readLine();
}

this works fine for the string part of my program but i also need to get the individual characters any help or links to examples etc would again begreat thanks.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Files - reading in characters Posted: Mar 23, 2002 10:51 AM
Reply to this message Reply
If you want to read in a character at a time with your buffered reader then you can do it as follows by using read() method. Note that read method returns int and you will have to cast it to char.
int c ;
char achar ; // Your character
while ( ( c = in.read() ) != -1 ) {
// C has your charatcer.Just convert it into char
achar = (char) c ;
}
Alternatively, you can read the line as a string as you are doing and then you can convert the string in char array as in :
char[] cArr = line.toCharArray() ;

or by calling getChars () method of String class. You can also use charAt() method of String to access a character in String.

Thanks
Kishori

Bodkins

Posts: 5
Nickname: bodkins
Registered: Mar, 2002

Re: Files - reading in characters Posted: Mar 23, 2002 12:02 PM
Reply to this message Reply
Thanks i was wondering about the syntax mainly, but it works now and reads strings and characters, thanks a lot.

Flat View: This topic has 5 replies on 1 page
Topic: jpeg image encoding Previous Topic   Next Topic Topic: Newbie Print question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use