Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #21722

Re: Reading a File

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.programmer
Subject Re: Reading a File
Date Fri, 25 Jan 2013 21:33:16 -0800
Organization A noiseless patient Spider
Lines 57
Message-ID <kdvpqt$539$1@dont-email.me> (permalink)
References <4c68b43b-ee08-48fc-91fe-7f3f81213f69@vb8g2000pbb.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Sat, 26 Jan 2013 05:33:17 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="aba33539224e5c782fe0c4053f7756fd"; logging-data="5225"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/wSRPZhrtes9Q/LBat6b8p"
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2
In-Reply-To <4c68b43b-ee08-48fc-91fe-7f3f81213f69@vb8g2000pbb.googlegroups.com>
Cancel-Lock sha1:AbIDTGVe0SOJrzJE5vpPfsfh4Wg=
Xref csiph.com comp.lang.java.programmer:21722

Show key headers only | View raw


On 1/25/2013 8:58 PM, Subhabrata wrote:
> Dear Group,
>
> I am a new learner in Java. And I wanted to open a file and read its
> content.
> The standard tutorials give long code. They use try,catch etc. I
> wanted something simple and started to experiment something as
> follows. But it was giving some error.
>
> import java.io.*;
> public class FileOutput  {
>
> 	/**
> 	 * @param args
> 	 */
> 	public static void main(String[] args)  {
> 		FileInputStream fstream = new FileInputStream("C:\\FileIO\
> \javadict.txt");
> 		DataInputStream in = new DataInputStream(fstream);
> 		BufferedReader br = new BufferedReader(new InputStreamReader(in));
> 		String strLine;
> 		System.out.println(br.strLine);
> 	  }
>
> 		// TODO Auto-generated method stub
>
> 	}
>
> If any one in the room can kindly suggest.
>
> Regards,
> Subhabrata.
> NB: Thank you all for your earlier solution though I worked my own
> with .equals only.
>

import java.io.*;

public class ReadIt {
     public static void main(String[] args) throws IOException {
         FileReader fr = new FileReader("ReadIt.java");
         BufferedReader br = new BufferedReader(fr);

         String str;
         while ((str = br.readLine()) != null)
             System.out.println(str);

         br.close();
     }
}

You will need to catch exceptions in real code but this will work unless 
an exception is thrown.

-- 

Knute Johnson

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Reading a File Subhabrata <subhabangalore@gmail.com> - 2013-01-25 20:58 -0800
  Re: Reading a File Knute Johnson <nospam@knutejohnson.com> - 2013-01-25 21:33 -0800
    Re: Reading a File subhabangalore@gmail.com - 2013-01-25 21:53 -0800
      Re: Reading a File Lew <lewbloch@gmail.com> - 2013-01-25 22:48 -0800
        Re: Reading a File Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-01-26 08:01 -0400
          Re: Reading a File Lew <lewbloch@gmail.com> - 2013-01-26 10:12 -0800
          Re: Reading a File Knute Johnson <nospam@knutejohnson.com> - 2013-01-26 11:41 -0800
            Re: Reading a File Lew <lewbloch@gmail.com> - 2013-01-26 13:01 -0800
  Re: Reading a File Roedy Green <see_website@mindprod.com.invalid> - 2013-01-26 02:47 -0800

csiph-web