How to read a file?

How do i calculate and read file in java--- I JUS NEED LITTLE HINTS TO CALCULATE THE LINE AND READ IT?

  • I WROTE THIS SO FAR AND ITS WORKING AND OPENING ANY FILE BUT I WANT TO CALCULATE AND READ MY FILES LINE BY LINE... CAN I GET SOME HELP HOW DO I DO THIS import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; import java.util.Scanner; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class heptathlon { public static void main(String[] args) throws Exception { JFileChooser fc = new JFileChooser(); if (fc.showOpenDialog(null)== JFileChooser.APPROVE_OPTION) { java.io.File file = fc.getSelectedFile(); Scanner input = new Scanner(file); while (input.hasNext()) { System.out.println( input.nextLine()); } input.close(); } else { System.out.println("NO FILE SELECTED"); } } }

  • Answer:

    Allocate a String object before the loop, like this: String sTmp = ""; In the loop where you have: System.out.println( input.nextLine()); Change it to these two lines:   sTmp = input.nextLine();   read the next line into 'sTmp'   System.out.println( sTmp );

Chris C at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.