kvarnerexpress 0 Report post Posted December 27, 2004 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
karlo 0 Report post Posted December 27, 2004 Use PHP! It's a lot more easy and fast! Share this post Link to post Share on other sites
cse-icons 0 Report post Posted December 28, 2004 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
spirit_valley 0 Report post Posted December 28, 2004 we are starting to learn how to write and read strings to/from txt files. <{POST_SNAPBACK}> Excuse me... but what language do you mean exactly? Use PHP! It's a lot more easy and fast! <{POST_SNAPBACK}> People have their own reasons why they don't use PHP. Share this post Link to post Share on other sites
Xedos 0 Report post Posted December 29, 2004 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
csmith 0 Report post Posted September 25, 2005 (edited) 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 September 25, 2005 by cmatcmextra (see edit history) Share this post Link to post Share on other sites
Dragonfly 0 Report post Posted September 25, 2005 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
beeseven 0 Report post Posted September 26, 2005 Ever think that not all programming languages are online?... I know it's hard to imagine, but it's true 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
dul 0 Report post Posted October 19, 2005 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