Jul 26, 2008

Reading From A Text File

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

free web hosting

Reading From A Text File

kvarnerexpress
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

Reply

karlo
Use PHP! It's a lot more easy and fast! tongue.gif

Reply

cse-icons
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...

Reply

spirit_valley
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.

Reply

Xedos
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.

Reply

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

 

 

 


Reply

Dragonfly
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.

Reply

beeseven
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();
    ...
}

Reply

dul
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) {
    }



Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : reading text file

  1. Text Image Java Problem - How to include text and image in some scrollable container? (1)
  2. How Do You Make Text In A <div> Change When You Click On A Link? - something to do with jvascript (4)
    Say I have a link and some text. How would I make it so that when I clicked on the link, that text
    changed?...
  3. Writing & Reading Files With Jsp - How to do it? (1)
    I've a project on creating a Movie online booking website that is powered with JSP. Now I'm
    trying to write and read from the file I wrote, but didn't know how. Can anyone with such
    expertise please teach me.. It has to be done with JSP. Thanks in advance.....
  4. Link In Search Form - Open a link inside a text area... (2)
    i added a javascript search to my site Search basically i wanted to have links on there so that
    when u click the link it runs the search. instead of making ppl type it into the textarea and
    clicking go. or have it so when u click the link it enters text into the text area and then
    searchs... anyone know what im chatting about? ~thanks Please make sure that Topic title has to
    be descriptive. ...
  5. Writing A Text File On Wen Host - write to the text file on the webhost (1)
    Hi! i need to write a text file with applet on the wen host. It is like a guest book that the
    visitor enters his comments and the comments are stored in the text file. I found a website which
    presents a method to write text files to the visitor's computer but i need to write the text
    file to the webhost the applet is hosted. its like a log file or a guest book. I know there is a
    way and it can be done but i can't figure it out! Any help would be appreciated!
    Thanks! kvarnerexpress Topic title and description are VERY import to this forum. Edit ...
  6. File Upload Script In Java - File Upload Script in JAVA (4)
    Hi, I am developing my personal website in JSP. I need a file upload script written in JAVA. I got a
    script that is Upload Bean from javazoom.com but the problem is its working fine on Windows
    Operating SYstem but not working on LINUX server. When i pass LINUX path (like
    /home/usr/public_html) to bean it gives me error Check UploadBean properties in your JSP. I
    think this bean aspecting a windows path. Can anyone provide me JSP Smart Upload script or any
    other file upload bean in JAVA. For your information Smart Upload website has been closed. My
    email id is ...
  7. How To Store The Text Typed In A Form? - sort of newsletter subsciption form (5)
    i would like to add a form on my site, that let's you fill in your e-mail address in order to
    subscribe to a newsletter. Actually, i just need to see their addresses somewhere (in a textfile,
    database, forward to my e-mailaddress,..). How can this be done? I mean, which script do i have to
    use? There is a database available. Php is enabled. I have no acces to cgi-bin, if that's
    needed. I hope someone can help me with this. The alternative would be that there is a link on my
    site so that they can mail it to me, but prefer a script /smile.gif' border='0' styl...
  8. Onclick Image ->> Add Text To Input(text)field? - (3)
    when some images get clicked, a text should added to an input(textfield). Does someone know how this
    can be done? I suppose it can be done via javascript. thanks in advance! /cool.gif' border='0'
    style='vertical-align:middle' alt='cool.gif' /> If you need more information, just ask
    /tongue.gif' border='0' style='vertical-align:middle' alt='tongue.gif' /> ...
  9. Where Do I Put The Text? - (0)
    I actually downloaded this menu applet from Magic Rainbow Buttons I made my own image to use for
    the eye HERE My question is how do I insert text? and where exactly?? here's the source:
    <!--param name=transition-frames value=20> HERE'S The text edit I
    use: // Create a menu.txt file using the index editor "text:" "1" "" "" "" "text:" "1" "" "" ""
    "text:" "1" "" "" "" "text:" "1" "" "" "" "text:" "1" "" "" "" "text:" "1" "" "" "" "text:" "1" ""
    "" "" "text:" "1" "" "" "" "text:" "1" "" "" "" "text:" "1" "" "" ""...
  10. Glowing text on mouseover... - java scripting .. (4)
    One more script for today ... /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0"
    alt="biggrin.gif" /> The text glows on mouseover of the page which will draw the eyes of your
    visitor. Good to use on important lines of text that you want noticed. The colors can be changed
    easily to blend in with your background color, as can be the font style and size. Installation:
    STEP 1: Copy the code below and paste this into the of your html document. Comments have been
    inserted where you change the text size, type and colors at the top part of the scr...



Looking for reading, text, file

Searching Video's for reading, text, file
advertisement



Reading From A Text File



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE