Question:
simplify this java code (displaying contents of Data.txt)?
chambli
2010-01-27 00:12:15 UTC
This code will read the Data.txt and print its content on the console.

but this code is so much complicated for me is there another code that i can use just to display contents of myfile "Data.txt".. ??

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* This program reads a text file line by line and print to the console. It uses
* FileOutputStream to read the file.
*
*/
public class FileInput {

public static void main(String[] args) {

File file = new File("Data.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;

try {
fis = new FileInputStream(file);

// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {

// this statement reads the line from the file and print it to
// the console.
System.out.println(dis.readLine());
}

// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

thanks..
Three answers:
?
2010-01-27 01:11:16 UTC
To read and display a simple text file:



import java.io.*;

import java.util.*;

..

public static void main(String[] args) {



String line = null;



try {



/*

Creates a new Scanner object from the given file name

The text file should reside in the same folder as your java class file.

*/

Scanner in = new Scanner(new File("Data.txt"));



/*

The loop reads the file line by line. The loop will continue to read as long as there are more lines to read

*/

while (in.hasNextLine()) {



/*

The line read is returned as a String and is stored in the variable line

*/

line = in.nextLine();



/*

Using the standard output stream each line is displayed as it is read to the console

*/

System.out.println(line);



}



/*

Display error message if an error occurs

*/

} catch (IOException ex) {



System.out.println("Error opening the file");



} catch (Exception ex) {



System.out.println("Error while reading from file");



} finally {



/*

Close the scanner once read is finished

*/

in.close();



}



}



-----------------------------------------

I would advise you to learn the code you have and understand how different streams work an learn their pros and cons.



Learning just the simple code may not always be a good thing, when you really need to come up with a solution that can require more complexity.



So try to learn variety of ways to solve problems in programming
Kardinal
2010-01-27 00:50:26 UTC
No dice... Simpler would be to right clic on data.txt and open it using Notepad.
icymidnight
2010-01-27 00:24:41 UTC
That's about as simple as it gets in Java. It's well commented, so you should be able to figure it out and modify it for your own purposes.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...