Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Reading From A Text File
kvarnerexpress
post Dec 27 2004, 12:16 AM
Post #1


Super Member
*********

Group: Members
Posts: 407
Joined: 13-December 04
Member No.: 2,696



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 and
90 <--this one
12
6
-9

how would i go baout doin this.
thnks

ke
Go to the top of the page
 
+Quote Post
karlo
post Dec 27 2004, 06:55 AM
Post #2


Privileged Member
*********

Group: Members
Posts: 618
Joined: 30-October 04
From: Philippines
Member No.: 2,049



Use PHP! It's a lot more easy and fast! tongue.gif
Go to the top of the page
 
+Quote Post
cse-icons
post Dec 28 2004, 05:54 AM
Post #3


Super Member
*********

Group: Members
Posts: 351
Joined: 19-October 04
From: India
Member No.: 1,824



I suppose the following is what u need...

CODE

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());


you can read any no.. of lines using the br reader.
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...
Go to the top of the page
 
+Quote Post
spirit_valley
post Dec 28 2004, 06:11 AM
Post #4


Member [Level 1]
****

Group: Members
Posts: 55
Joined: 20-December 04
From: Hong Kong
Member No.: 2,816



QUOTE(kvarnerexpress @ Dec 27 2004, 08:16 AM)
we are starting to learn how to write and read strings to/from txt files.
*


Excuse me... but what language do you mean exactly?

QUOTE(karlo @ Dec 27 2004, 02:55 PM)
Use PHP! It's a lot more easy and fast! tongue.gif
*


People have their own reasons why they don't use PHP.
Go to the top of the page
 
+Quote Post
Xedos
post Dec 29 2004, 05:52 AM
Post #5


Give me Reputation and i'll give you some back.
Group Icon

Group: Banned
Posts: 203
Joined: 29-December 04
From: Wirral, Northwest England
Member No.: 3,000



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.
Go to the top of the page
 
+Quote Post
csmith
post Sep 25 2005, 07:20 AM
Post #6


Newbie [Level 1]
*

Group: Members
Posts: 11
Joined: 25-September 05
Member No.: 12,225



The above code in the previous message would work:

QUOTE
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:

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!!


This post has been edited by cmatcmextra: Sep 25 2005, 07:38 AM
Go to the top of the page
 
+Quote Post
Dragonfly
post Sep 25 2005, 08:15 AM
Post #7


Privileged Member
*********

Group: Members
Posts: 702
Joined: 17-February 05
Member No.: 3,817



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.
Go to the top of the page
 
+Quote Post
beeseven
post Sep 26 2005, 09:29 PM
Post #8


Privileged Member
*********

Group: Members
Posts: 629
Joined: 26-February 05
Member No.: 3,995



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

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):
CODE
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();
    ...
}
Go to the top of the page
 
+Quote Post
dul
post Oct 19 2005, 01:00 AM
Post #9


Member [Level 1]
****

Group: Members
Posts: 73
Joined: 21-September 05
Member No.: 12,113



QUOTE(kvarnerexpress @ Dec 27 2004, 12:16 AM)
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) {
    }


Go to the top of the page
 
+