Jump to content
xisto Community
kvarnerexpress

Reading From A Text File

Recommended Posts

Reading from a text file whats up guys, got some more questions. we are starting to learn how to write and read strings to/from txt files. wel thats pretty easy, but how would i retrieve only the first 2 lines of the txt file, for example, if i wanted the first 2 lines, not any other, how would i do it:12 <--i only need this one and90 <--this one126-9how would i go baout doin this.thnkske

Share this post


Link to post
Share on other sites

I suppose the following is what u need...

[br]File inputFile = new File("C:\\test.java"); // put ur filename here[/br]    [br]    BufferedReader br = new BufferedReader(new FileReader(inputFile));[/br]    [br]    System.out.println(br.readLine());[/br]   System.out.println(br.readLine());[br]

you can read any no.. of lines using the br reader. [/br]There are otherways too in which you can read files in java...
I suppose u wanted java coz... this is a Java forum...
there are other methods like readLine, readInt, readLong etc.. which you can explore as per your requirements. Go thru the api of the java.io package classes.
Hope this is helpful...

Share this post


Link to post
Share on other sites

If not php, use perl. Its all good. The main goal in any language is to let you do the most things in the least set of lines. I say PHP. PHP is the best language for anything and alot simpler. I also see PHP as more wildy documented and theres alot more support. Go the Smart Way, Use PHP.

Share this post


Link to post
Share on other sites

The above code in the previous message would work:

File inputFile = new File("C:\\test.java"); // put ur filename here   
    BufferedReader br = new BufferedReader(new FileReader(inputFile));
   
    System.out.println(br.readLine());
  System.out.println(br.readLine());


However, if you wish to read only a certain number of lines, you could also add the following code:

try {        File inputFile = new File("C:\\test.java"); // put ur filename here        BufferedReader in = new BufferedReader(new FileReader("infilename"));        String str;        int counter = 0;        while (((str = in.readLine()) != null) && i < 2) {  //set i < the number of lines which you wish to read - i.e., if you wish to read the //first 10 lines only, you would set i < 10            System.out.println(str);             i++;          }        in.close();    } catch (IOException e) {    }

Notice from cmatcmextra:
Use quote tags when copying and code tags when using code!!

Edited by cmatcmextra (see edit history)

Share this post


Link to post
Share on other sites

Yes, using programming language for a site is always better if the site has to be dynamic.. otherwise plain html pages are the best. they are so friendly with search engines. I don't quite understand text files the thread starter is talking about.

Share this post


Link to post
Share on other sites

Ever think that not all programming languages are online?... I know it's hard to imagine, but it's true :ph34r:

BufferedReaders are good, but if you're using Java 1.5.0 then you can use Scanners which are a bit easier. Here's code to read from a file with a scanner (you also have to import java.util.Scanner and java.io.File):

public static void main(String[] args) throws FileNotFoundException //because you're using the File class{     Scanner infile = new Scanner(new File("filename.txt"));     String s1 = infile.next(); //you could use int n = infile.nextInt() if you want an int     String s2 = infile.next();     infile.close();     ...}

Share this post


Link to post
Share on other sites

try {        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while (str != null) {
            System.out.print("> prompt ");
            str = in.readLine();
            process(str);
        }
    } catch (IOException e) {
    }


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.