Home Discussion Discuss: Java Read File Line by Line - Java Tutorial

  • shortlyhoussam April 14, 2011 at 8:05 AM

    FileReader inFile = new FileReader(filename); BufferedReader bufRead = new BufferedReader(inFile); while ((line = bufRead.readLine()) != null) { }

  • computertaniya tripathi April 16, 2011 at 3:00 PM

    diference between internet and intranet

  • appriciationsmohan May 26, 2011 at 11:54 PM

    Thanks a lot to Rose India

  • javalakshmanan June 21, 2011 at 9:50 AM

    import java.io.*; class FileRead { public static void main(String args[]) { try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("textfile.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); } //Close the input stream in.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } } in the above program why we need to use the BufferedReader Class? can u explain?

  • I love youCaitlyn June 24, 2011 at 6:26 AM

    I owe my first child to you. Without this I would've jumped off a five foot bridge.

  • How to Can I read previous lineJames June 24, 2011 at 6:35 PM

    Is there anyway to read previous line? for example I want to compare last line in file with certain value. how would I know that I've reached the last line?

  • Copy file to another with readlineAnonymous June 26, 2011 at 12:32 PM

    String line = null; boolean firsttime = true; while ((line = input.readLine()) != null) { if (!firsttime) { contents.append(System.getProperty("line.separator")); firsttime = false; } mystringbuffer.append(line); }

  • WhyAshot July 12, 2011 at 10:14 AM

    DataInputStream in = new DataInputStream(fstream); Why do we need this? I tired code without it worked just as fine!

  • read and write a fileBhuvi June 10, 2013 at 1:56 PM

    Any one pls help me... I want 2 read a file and write the "lines which are all contains the word error" into other file...... How can i fetch the particular lines which has the word error???

  • javaaba duncan August 24, 2011 at 7:43 PM

    Please at run time there is no command to open and read the file. Can u help me

  • Thanks!AndReS August 30, 2011 at 12:16 AM

    Hey, the code worked like a charm without a single change. Thanks a lot!

  • Not so importantAnie September 22, 2011 at 6:45 PM

    Hey post some effective code not so good dude

  • Main Programyaya September 28, 2011 at 10:08 AM

    Hai... I have some problem with my project.can you help me? I didn't know why it cann't run.the problem in browse data. please help me. here the codes. package Yaya; import gov.nasa.worldwind.WorldWindow; import defaProject.FFrame; import gov.nasa.worldwind.geom.Position; import java.util.Vector; public class GUI extends javax.swing.JFrame { WorldWindow wwd; BacaFile bf; MainProgram mp; SatuanPosisi sp; Posisi position1, position2, position3, positionP; FFrame fFrame; private Vector<String[]> data1, data2, data3; /** Creates new form GUI */ public GUI(FFrame fFrame) { initComponents(); mp = new MainProgram(); this.fFrame = fFrame; Lat1.setText("-0.70417");Lon1.setText("119.85528"); Lat2.setText("-0.6");Lon2.setText("119.7"); Lat3.setText("1.19139");Lon3.setText("121.4225"); LatP.setText("-0.87639");LonP.setText("119.83861"); this.setTitle("Penentuan Ketinggian Air Laut"); } private GUI() { throw new UnsupportedOperationException("Not yet implemented"); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); Lat1 = new javax.swing.JTextField(); Lat2 = new javax.swing.JTextField(); Lat3 = new javax.swing.JTextField(); Lon1 = new javax.swing.JTextField(); Lon2 = new javax.swing.JTextField(); Lon3 = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); LatP = new javax.swing.JTextField(); LonP = new javax.swing.JTextField(); button2 = new javax.swing.JButton(); button1 = new javax.swing.JButton(); nameFile2 = new javax.swing.JLabel(); nameFile1 = new javax.swing.JLabel(); Run = new javax.swing.JButton(); button3 = new javax.swing.JButton(); nameFile3 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setResizable(false); jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153))); jLabel1.setText("Lokasi 1 :"); jLabel2.setText("Lokasi 2 :"); jLabel3.setText("Lokasi 3 :"); jLabel4.setText("Latitude"); jLabel5.setText("Longitude"); jLabel6.setText("Lokasi P :"); LonP.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { LonPActionPerformed(evt); } }); button2.setText("Browse"); button2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { browseLokasi(evt); } }); button1.setText("Browse"); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { browseLokasi(evt); } }); nameFile2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); nameFile2.setText(". . ."); nameFile2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102))); nameFile1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); nameFile1.setText(". . . "); nameFile1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102))); nameFile1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); Run.setText("Run"); Run.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { Excecute(evt); } }); button3.setText("Browse"); button3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { browseLokasi(evt); } }); nameFile3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); nameFile3.setText(". . ."); nameFile3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102))); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE) .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(LatP, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(10, 10, 10) .addComponent(LonP)) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(Lat2) .addComponent(Lat1, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE) .addComponent(Lat3, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(10, 10, 10) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(Lon1) .addComponent(Lon3, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Lon2, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(jLabel4) .addGap(80, 80, 80) .addComponent(jLabel5) .addGap(39, 39, 39))) .addGap(27, 27, 27) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(button3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(button2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(Run, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(button1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)) .addGap(24, 24, 24) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(nameFile3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(nameFile2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(nameFile1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)) .addGap(31, 31, 31)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(button1) .addComponent(nameFile1, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(15, 15, 15) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(button2) .addComponent(nameFile2, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Lat1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1) .addComponent(Lon1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Lon2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Lat2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Lat3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Lon3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3) .addComponent(button3) .addComponent(nameFile3, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(14, 14, 14) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(LatP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(LonP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(Run))) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(20, 20, 20) .addComponent(jLabel6))) .addContainerGap(14, Short.MAX_VALUE)) ); button2.getAccessibleContext().setAccessibleParent(jPanel1); button1.getAccessibleContext().setAccessibleParent(jPanel1); Run.getAccessibleContext().setAccessibleParent(jPanel1); button3.getAccessibleContext().setAccessibleParent(jPanel1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void Excecute(java.awt.event.ActionEvent evt) { LeastSquare ls; positionP = sp.getData(3); double jarak1P = KonversiJarak.getJarak(position1.getLat(), positionP.getLat(), position1.getLon(), positionP.getLon()); double jarak2P = KonversiJarak.getJarak(position2.getLat(), positionP.getLat(), position2.getLon(), positionP.getLon()); double jarak3P = KonversiJarak.getJarak(position3.getLat(), positionP.getLat(), position3.getLon(), positionP.getLon()); double[] x = {jarak1P, jarak2P, jarak3P}; int numHari = data1.size(); int numJam = data1.elementAt(1).length; for (int i = 0; i < numHari; i++){ String[] temp1 = data1.get(i); String[] temp2 = data2.get(i); String[] temp3 = data3.get(i); for (int j = 0; j < numJam; i++){ double lev1 = Double.parseDouble(temp1[j]); double lev2 = Double.parseDouble(temp2[j]); double lev3 = Double.parseDouble(temp3[j]); double[] y = {lev1, lev2, lev3}; ls = new LeastSquare(x, y); double d = ls.getElevasi(0.0); } } } public void excecute() { mp.setDataP(Double.parseDouble(LatP.getText()),Double.parseDouble(LonP.getText())); mp.run(jPanel1); } private void browseLokasi(java.awt.event.ActionEvent evt) { bf = new BacaFile(); if (evt.getSource().equals(button1)){ data1 = bf.readElementByElement(); nameFile1.setText(bf.getNameFile()); position1 = sp.getData(1); fFrame.createIcon("stasiun", Position.fromDegrees(position1.getLat(), position1.getLon())); } else if (evt.getSource().equals(button2)){ data2 = bf.readElementByElement(); nameFile2.setText(bf.getNameFile()); position2 = sp.getData(2); fFrame.createIcon("stasiun", Position.fromDegrees(position2.getLat(), position2.getLon())); } else if (evt.getSource().equals(button3)){ data3 = bf.readElementByElement(); nameFile3.setText(bf.getNameFile()); position3 = sp.getData(3); fFrame.createIcon("stasiun", Position.fromDegrees(position3.getLat(), position3.getLon())); } } private void LonPActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new GUI().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JTextField Lat1; private javax.swing.JTextField Lat2; private javax.swing.JTextField Lat3; private javax.swing.JTextField LatP; private javax.swing.JTextField Lon1; private javax.swing.JTextField Lon2; private javax.swing.JTextField Lon3; private javax.swing.JTextField LonP; private javax.swing.JButton Run; private javax.swing.JButton button1; private javax.swing.JButton button2; private javax.swing.JButton button3; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JPanel jPanel1; private javax.swing.JLabel nameFile1; private javax.swing.JLabel nameFile2; private javax.swing.JLabel nameFile3; // End of variables declaration public Posisi getData(int index) { if (index==0) return Posisi.set(Double.parseDouble(LatP.getText()), Double.parseDouble(LonP.getText())); else if (index==1) return Posisi.set(Double.parseDouble(Lat1.getText()), Double.parseDouble(Lon1.getText())); else if (index==2) return Posisi.set(Double.parseDouble(Lat2.getText()), Double.parseDouble(Lon2.getText())); else if (index==3) return Posisi.set(Double.parseDouble(Lat3.getText()), Double.parseDouble(Lon3.getText())); return null; } }

  • This codeBen February 21, 2013 at 7:48 PM

    This was really helpful, thanks.

  • convaluted code isn't it?Neil December 7, 2011 at 4:23 AM

    Can't you just use: BufferedReader reader = new BufferedReader(new FileReader(filename)); to create the reader instead of all that guff?

  • Awesome.Gaurav Chauhan December 14, 2011 at 2:12 PM

    Thanks for this program. Its really knowledgeable.

  • FileReaderHerock Hasan December 14, 2011 at 8:52 PM

    Excellent article and so much helpful. Hope another helpful content will also get from this website

  • a questionfrank January 9, 2012 at 8:52 AM

    will the above code, will be faster for reading a file whose size is greater than 200MB????

  • FOR JAVA TUTORIALMANOJ SINGHAL January 11, 2012 at 7:38 PM

    Pls send me the complete java tutorial and updatation.

  • File ReadingCrypt0s January 14, 2012 at 12:11 AM

    There is a much easier way to do this, and in one line: String line = ""; BufferedReader input = new BufferedReader(new FileReader("filename")); while ((line = input.readLine()) != null){ System.out.println(line); } //remember to close our streams or we run into issues! input.close();

  • Thank YouRachel January 19, 2012 at 11:12 PM

    Great site. Always been helpful.

  • how to run this programvani January 23, 2012 at 5:49 PM

    dear sir/madam, could you plz explain me the procedure to run this program Thanks vani

  • location of text filenikhil February 9, 2012 at 3:37 PM

    i am using ntebeans on windows..... so where do i place my text file so that the object reads it?????

  • errorNikhil February 9, 2012 at 8:46 PM

    the above code is giving a error of not recognizing the "in" symbol in netbeans.................please help

  • filereadanilkumar February 11, 2012 at 9:03 AM

    very easy program filereader

  • can you help mejAz February 18, 2012 at 2:45 PM

    can you help me making my thesis... music streaming via bluetooth. thankz...

  • Read text fileJemmy Adams March 5, 2012 at 1:25 AM

    Sirs; Please, send step by step how can I read a text file, I am a mechanical engineer, not a programmer or IT. Thanks Jemmy

  • xXVNjsHniYvnVadim March 22, 2012 at 9:32 AM

    Android system does not urasnetdnd PE format. so called windows exe files are in this format (PE stands for Portable Executable but it is not so portable after all!). likely Android executable frmat is ELF, a well-known widely used (among several systems) codes container .moreover, the code into windows executable files is for x86 processors (or for CLR, not JVM), and Android hardware for sure has no x86 processors and afaik no x86-opcode compatible processor too.so basically, it's nonsense converting an exe (specific format, specific system, specific machine code for specific processor, specific library bindings) into a file Android can read' (oh, as passive data you can transfer the exe to android as it is, but forget it can run it or even see that there's code inside! just data to it!) Was this answer helpful?

  • awesomekok1n April 5, 2012 at 7:55 PM

    thanks a lot, just what i needed!

  • hipankaj April 20, 2012 at 7:15 PM

    thanks for this,it helped a lot.

  • ThanksGabriel Alejandro Matossian May 9, 2012 at 9:39 PM

    I am very grateful, your article really helped me. Kind regards, Gabriel

  • Deprecation errorPratibha K June 2, 2012 at 11:26 AM

    for the line "while (( thisLine = br. readLine()) != null) { the dot after br is coming wrong in deprecation error. pls help me to solve this error.

  • A little errorFerdinand June 13, 2012 at 4:01 PM

    You also need to add "outFile.close();" after "in.close();" Otherwise the last chunk gets chopped off and lost.

  • ThankIman June 15, 2012 at 6:37 PM

    Thank you for code

  • ?Henry June 25, 2012 at 11:01 PM

    Where do you put the file???

  • FileReadPalle June 26, 2012 at 10:38 PM

    Hi Thank You very much ! I'm interested in Java programming. And I found Your example here. And I have downloaded it. I'm sure it will help me getting clever programming in Java. Yours sincerely Palle

  • IT 16iris potot June 29, 2012 at 2:16 PM

    give more examples!

  • nice tutorialkiper July 5, 2012 at 12:28 PM

    i tried it, no error :) i want to know, how to show the text file (*.txt) into form (textfield) and then import to mysql or another database

  • java using appletsakthi July 9, 2012 at 3:40 PM

    need java using applet login coding

  • I got an errorGadss July 19, 2012 at 7:55 AM

    hello, I try your code but I got this error: Error: textfile.txt (The system cannot find the file specified)

  • questionBayan Al-Atrash July 28, 2012 at 6:12 PM

    where must i save the file.txt??

  • Aishwarya Nathan August 8, 2012 at 4:27 PM

    The code really really works :-)

  • thanks─?├┤ng August 29, 2012 at 4:36 PM

    thanks so much

  • String:Aldrin September 11, 2012 at 9:09 AM

    Hw to read a file frm a txt document & according to the no: of characters in the individual word we have to append the words in the document & display in the output screen.

  • thankspradip garala September 21, 2012 at 9:44 PM

    Thanks for code...

  • thankspradip garala October 3, 2012 at 11:53 AM


  • its easy way nikhil October 27, 2012 at 12:46 PM

    /*FileInputStream fstream = new FileInputStream("textfile.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream);*/ //the above code can be avoided BufferedReader br = new BufferedReader(new FileReader("chits.txt"));

  • NEED HELP URGENTLYchief November 13, 2012 at 7:57 PM

    hi i have a file format like this 0274589654, 5 0245878968, 6 0278695455, -1 0245875258, .5 after reading from the file line by line. i want to read the first number into a variable and read the second number into another variable. need help urgently. thanks in advance for helping.

  • Alternative wayAnonymousOne November 23, 2012 at 1:40 AM

    Try to be more concise... One can also try this: BufferedReader in = new BufferedReader(new FileReader("textfile.txt")); instead of using InputStreamReader, DataInputStream and FileInputStream. Hope it helps.

  • Output Swathi December 4, 2012 at 12:18 PM

    Hi, Can u plz tel me,How to run this program? show me in cmd prompt,how to run??? reply me

  • Another Way to New a BufferedReaderasdfghjkl20203 April 11, 2013 at 4:22 PM

    Instead of new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(new File(fileName))))); The following is more clear: new BufferedReader(new FileReader(filename));

  • Good example of file readingJohn August 30, 2013 at 8:41 PM

    Hi, Thanks for posting such a nice tutorial. I was searching for the posts for reading a file which is very big in size, one line at a time. I also tried to read the complete file in memory but due to Java memory constraint it is not possible. So, I searched for the "Read file line by line in Java" on google and luckily found this tutorial. This post on reading file helped me a lot for solving my problem. After using the program here I am able to read big text file of around 3.5 GB without any error or memory issue. Program performance is also good. Thanks for such a nice post.

  • fileErick Sanvicente February 16, 2014 at 12:18 AM

    hello, where should i put the file.txt so the program can read it?

  • java filetanveer islam June 24, 2011 at 7:35 PM

    how to create file

  • HiJyothsna May 31, 2013 at 1:14 PM

    Hey it will read the data whatever u entered ok.But It didn't interact with File.So I am thinking it is not corrrect>Please forgive if there is wrong in my discussion

  • hiJyoths May 31, 2013 at 1:15 PM

    Sorry sorry I got thanq

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Related Articles
Mobile Applications Questions

objective c read file line by line
objective c read file line by line  Please explain how to read files in Objective C line by line 

Java Beginners

How to read a line from a doc file
How to read a line from a doc file  How to read a line from a doc file   Hi Friend, To run the following code, you need to download POI... void main(String[] args) { File file = null; WordExtractor extractor = null 

IoC Questions

How to extract a specific line from a text file? - IoC
How to extract a specific line from a text file?  Hi all, i'm trying to write a code that return a specific line from a text file. so my first simple code here allows me to read the content of my file Text.file 

Programming Tutorials

AJAX Line       AJAX Community with a blog, forum and tutorials Read full Description 

Development process Questions

line number in a file of entered string
line number in a file of entered string  when i entered string from console , it should show line number in a file of that string 

Java Technology Tutorials

Line by Line reading from a file using Scanner Class
In this section, you will get to know about reading line by line a text file using java.util.Scanner class 

Java Beginners

dead line tomorrow...help
dead line tomorrow...help  1)write a program to convert double to int and viceversa using rmi(remote method invocation) 2)write a program to download a video file and run it on your client machine using rmi 3)fibonacci 

C Tutorials

C file read example
C file read example       This section demonstrates you to read a line from the file. You can see in the given example, we prompt the user to enter the name of the file to read 

Java Beginners

Draw Line
Draw Line  sir i want to draw a moving line in j2me.That line should also show arrow in moving direction. How can we do so 

Java Beginners

line graph
line graph  I have developed an application and in that i have to compare 2 reports using line graph from the data taken from ms access database?please help 

JavaScript Questions

on line exam
on line exam  how we can upload the data on the server from any site 

PHP Questions

execute a PHP script using command line
script using command line?   Hi friends, Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line... and provide the PHP script file name as the command line argument. For example 

PHP Questions

How to Check Markup in PHP File from Command Line
How to Check Markup in PHP File from Command Line  Check Markup in PHP File from Command Line   PHP has a feature that allowed you to check... try this: http://www.phpzag.com/check-markup-in-php-file-from-command-line 


Printing Command Line arguments in JRuby
Printing Command Line arguments in JRuby       In this section of JRuby tutorial we will discuss about printing command line arguments in JRuby program. We can 

JSP Servlet Questions

JFreechart multi line chart help in JSP
JFreechart multi line chart help in JSP  Hi, I am am looking for and help in getting multi line chart with Jfree chart, i had manage to write a singe line chart but i need multy line can you please help. <%@page 

Java Interview Questions

Command line arguments
Command line arguments  Please explain me what is the use of "command line arguments" and why we need to use 

Java Beginners

Line From code
Line From code  document.getElementById("demo").innerHTML=Date(); Please explain each and every word in this line 


How to read text file in Servlets
it to the BufferedReader. A variable text is defined of String type. Read the file line... How to read text file in Servlets  ... file in servlets. In this example we will use the input stream to read the text 

PHP Tutorial

PHP line break, PHP nl2br() function
The PHP Line break example & code If you are developing the application... the same value on the website. If user enters the <enter key> to add the line... the text will displayed without any line break. You can use the nl2br() function 

PHP Questions

Line Breaks in PHP
Line Breaks in PHP  Hi, I am beginner in PHP language. How to do the line-break in PHP? Does somebody help me in this regard. Thanks 

JDBC Questions

read text file and store the data in mysql - JDBC
read text file and store the data in mysql  when we store the data in mysql table from text file its store the data from new line to new column. how to store the data in different column from a single line of text file.  

Applet Questions

Adding text in to text area of jframe at new line with different background colour
Adding text in to text area of jframe at new line with different background colour  Hi, I am reading text from text file and i want to write that text in to text area of jframe.But i am reading line by line from file and i want 

Java Beginners

Remove JTable row that read txt file records
Remove JTable row that read txt file records  Hi every one. i have a jtable that correctly read data frome file and show them in own. I want to add... RemoveRow() { String line; data = new Vector 

Java Beginners

sorting Line in c
sorting Line in c  Hi I need help with sorting line in programming c asking user to enter 1-10 characters and then sort them in lower case, alphabetical and ascending order. Any help will be appreciated! please help! void 

Java Beginners

divide the line in to two
divide the line in to two  In| this |example |we| are| creating |an |string| object| .We| initialize| this| string |object| as| "Rajesh Kumar"|.| We| are| taking| sub| string | In above mentioned line based on the tag i want 

Java Beginners

character as command line arguments
character as command line arguments  public class testing { public static void main(String args[]) { char a; a=args[0]; System.out.println("character is:"+a); } } what will be the above program output 

JSP Servlet Questions

Dynamic Line draw in JSp
Dynamic Line draw in JSp  In my application. I have one source selected from listbox and multiple targets selected from checkboxes. After submitting... multiple target. And each line should have dynamically link of other jsp page 

Java Beginners

What is wrong with this line
What is wrong with this line  What is wrong with this line JOptionPane.showMessageDialog(null? for Using Lewandowski's Savings pgrm!"); from if (input.equals("exit") || input.equals("Exit")) { JOptionPane.showMessageDialog 

JSP Servlet Questions

On line examination project
On line examination project  Hi, I am doing project in jsp,nam eis online examination project. I am facing some problems. In my database several questions. There should be one question in one page. And 2 button previous and next 

SQL Tutorial

Backup File With Separator
Backup File With Separator       Backup File With Separator in SQL  is used to indicate line and record separator. Understand with Example The Tutorial illustrate