Reading from file with DataInputStream is very slow - java

I have got a file containing a large amount of numbers.
I have tried to use the following code to read it from the file, but it is super slow anyone can help to reduce the time?
Following is my code to read it in a very slow way:
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
public class FileInput {
public static void main(String[] args) {
Scanner scan1 = new Scanner(System.in);
String filename = scan1.nextLine();
File file = new File(filename);
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) {
System.out.println(dis.readLine());
}
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Don't use a DataInputStream to read lines from a file. Instead, use a BufferedReader, as in:
fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
while ((String line = reader.readLine()) != null) {
System.out.println(line);
}
The javadoc on DataInputStream.readLine tells you to not use that method. (it's been deprecated)
Of course, when you actually get around to reading the numbers, I'd encourage you to forget reading the lines yourself, and just let Scanner read the numbers for you. If you need to know which numbers were on the same line, Scanner can do that for you too:
Scanner fileScanner = new Scanner(file, "UTF-8").useDelimiter(" +| *(?=\\n)|(?<=\\n) *");
while (fileScanner.hasNext()) {
List<Integer> numbersOnLine = new ArrayList<Integer>();
while (fileScanner.hasNextInt()) {
numbersOnLine.add(fileScanner.nextInt());
}
processLineOfNumbers(numbersOnLine);
if (fileScanner.hasNext()) {
fileScanner.next(); // clear the newline
}
}
That fancy regex makes it so that the newline characters between lines also show up as tokens to the Scanner.

It runs much faster on my machine with the println is commented out. Writing to the screen slows things down a lot. And that's not just a java thing...happens in C/C++ and every other language I've worked with.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class file {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
String fname = "";
System.out.print("File Name: ");
fname = keyboard.next();
try{
Scanner file1 = new Scanner(new FileReader(fname));
System.out.println("File Open Successful");
int length = file1.nextInt();
String[] content = new String[length];
for (int i=0;i<length;i++){
content[i] = file1.next();
}
for (int i=0;i<length;i++){
System.out.println("["+i+"] "+content[i]);
}
System.out.println("End of file.");
} catch (FileNotFoundException e){
System.out.println("File Not Found!");
}
}
}

Related

Reading from a file using ArrayList in Java

I have an assignment that ask me to read from a file, us an ArrayList to organize and declare the numbers, and then calculate the average of those numbers and print them in a new file. I know that I need 3 parts for this which would be the Reader, Writer and the Array List but i get an error when compiling when I try to read from the scaner. Can someone help with how to read from the file with the ArrayList and likewise, how to write into a new file.
import java.util.ArrayList;
import java.util.Collections;
import java.io.*; //Replaces the scanner
import java.io.BufferedReader;
import java.util.Scanner;
import java.io.FileReader; // Used by the BufferedReader import java.util.Scanner;
import java.io.FileNotFoundException; //
import java.io.IOException; //
class SD9 {
public static void main( String[] args ) {
try{
FileReader Fr = new FileReader( "Patriots.txt" );
// the file reader bridges the program and the .txt file together.
BufferedReader Br = new BufferedReader( Fr );
String line = Br.readLine();
// BufferredReaders can only read one line at a time.
FileWriter fw = new FileWriter( "PatriotsStat.txt" );
BufferedWriter bw = new BufferedWriter( fw );
while( line != null ) {
//BufferredReaders return null once they've reached the end of the file.
ArrayList<Double> Patriots = new ArrayList<Double>();
for(int i = 0; i < 23; ++i ) {
Patriots.add( scan.nextDouble() );
}
/* String Line1 = "2014 PreSeason:";
bw.write( " " );
bw.newLine();
/*String Line3 = " FinalAvg: " + finalAvg;
bw.write( Line3 );
bw.newLine();*/
}
bw.close();
}catch( FileNotFoundException F ) {
//.....
} catch( IOException I ) {
}
}
}
This should work:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
class SD9 {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("Patriots.txt"));
PrintWriter writer = new PrintWriter(new File("PatriotsStat.txt"));
ArrayList<Double> Patriots = new ArrayList<Double>();
double sum = 0;
while (scanner.hasNext()) {
double num = scanner.nextDouble();
sum += num;
Patriots.add(num);
}
scanner.close();
for (int i = 0; i < Patriots.size(); i++) {
writer.write(Patriots.get(i)+"\n");
}
double average = sum / Patriots.size();
writer.write("Average : "+average);
writer.close();
}
}
I believe this will help.
File fr = new File("Patriots.txt");
Scanner sc = new Scanner(fr);
ArrayList<Double> patriots = new ArrayList<Double>();
while(sc.hasNextDouble()){
patriots.add(sc.nextDouble);
}
sc.close();
The code is failing to compile because your code does not follow the correct Java syntax for a try...catch block
In Java, a try...catch block follows the following form:
try {
// do something...
} catch (Exception e) {
// handle the exception...
}
In your code, you will see that you have code in between your try block and your catch block:
The bw.close() line is the culprit.
try {
// code
}
bw.close();
} catch( FileNotFoundException F ) {
//.....
} catch( IOException I ) {
}
Assuming you are doing this in an IDE (NetBeans, Eclipse, etc.), this relevant information can be found in the 'Build Output' window

Java exception from scanning a file that was imported

I am trying to make a program that imports a text file and analyzes it to tell me if another text file has possible match up sentences. I keep running into this error when I import my file and attempt to analyze it. I am assuming that I am missing something in my code.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at PossibleSentence.main(PossibleSentence.java:30)
Heres my code too:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class PossibleSentence {
public static void main(String[] args) throws FileNotFoundException{
Scanner testScan = new Scanner(System.in);
System.out.print("Please enter the log file to analyze: ");
String fileName = testScan.nextLine();
File f = new File(fileName);
Scanner scan = new Scanner(f);
String line = null;
int i = 0;
while (scan.hasNextLine()) {
String word = scan.next();
i++;
}
scan.close();
File comparative = new File("IdentifyWords.java");
Scanner compare = new Scanner(comparative);
String line2 = null;
}
}
The second scanner I havent completed yet either. Any suggestions?
We need more info to conclusively answer, but check out the documentation for next(). It throws this exception when there's no next element. My guess is it's because of this part:
String fileName = testScan.nextLine();
You're not checking if hasNextLine first.
You are passing a file argument to a Scanner object, try using an InputStream
File input = new File(/* file argument*/);
BufferedReader br = null;
FileReader fr= null;
Scanner scan = null;
try {
fr = new FileReader(input);
br = new BufferedReader(fr);
scan = new Scanner(br);
/* Do logic with scanner */
} catch (IOException e) {
/* handling for errors*/
} finally {
try {
if (br != null) {
br.close();
}
if (fr != null) {
fr.close();
}
if (scan != null) {
scan.close();
}
} catch (IOException e) {
/* handle closing error */
}
}

How to read a file, reverse the order, and write reverse order

Like a similar project I made, this project is reading characters from a txt file, reversing the order of the string and rewriting it to another txt file. But it keeps outputting my exception of "Something went wrong". Can anyone help me fix what is going wrong?
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ReverseFile
{
public static void main(String[] args) throws IOException
{
try{
String source = args[0];
String target = args[1];
File sourceFile=new File(source);
Scanner content=new Scanner(sourceFile);
PrintWriter pwriter =new PrintWriter(target);
while(content.hasNextLine())
{
String s=content.nextLine();
StringBuffer buffer = new StringBuffer(s);
buffer=buffer.reverse();
String rs=buffer.toString();
pwriter.println(rs);
}
content.close();
pwriter.close();
System.out.println("File is copied successful!");
}
catch(Exception e){
System.out.println("Something went wrong");
}
}
}
So here is the information from the stacktrace:
java.lang.ArrayIndexOutOfBoundsException: 0
at ReverseFile.main(ReverseFile.java:36)
i am not so sure about your environment, and how long the text might be. and i am also not so sure why you need a scanner?
anyway, here's my take on the problem, hope this helps you :)
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
public class Reverse {
public static void main(String[] args) {
FileInputStream fis = null;
RandomAccessFile raf = null;
// by default, let's use utf-8
String characterEncoding = "utf-8";
// but if you pass an optional 3rd parameter, we use that
if(args.length==3) {
characterEncoding = args[2];
}
try{
// input file
File in = new File(args[0]);
fis = new FileInputStream(in);
// a reader, because it respects character encoding etc
Reader r = new InputStreamReader(fis,characterEncoding);
// an outputfile
File out = new File(args[1]);
// and a random access file of the same size as the input, so we can write in reverse order
raf = new RandomAccessFile(out, "rw");
raf.setLength(in.length());
// a buffer for the chars we want to read
char[] buff = new char[1];
// keep track of the current position (we're going backwards, so we start at the end)
long position = in.length();
// Reader.read will return -1 when it reached the end.
while((r.read(buff))>-1) {
// turn the character into bytes according to the character encoding
Character c = buff[0];
String s = c+"";
byte[] bBuff = s.getBytes(characterEncoding);
// go to the proper position in the random access file
position = position-bBuff.length;
raf.seek(position);
// write one or more bytes for the character
raf.write(bBuff);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// clean up
try {
fis.close();
} catch (Exception e2) {
}
try {
raf.close();
} catch (Exception e2) {
}
}
}
}
You need to specify the filenames(source and target) on command-line, while running the program.
java ReverseFile source.txt target.txt
In your program, you try to read the name of files from command-line as
String source = args[0];
String target = args[1];
So if you do not specify those names there, java tries to access the array args at index 0 and 1 which are empty and you get ArrayIndexOutOfBoundsException.
here is ur error free solution to ur problem,u were using "Scanner" without importing "util"
package.here we go:-----------
import java.io.*;
import java.util.*;
public class ReverseFile
{
public static void main(String[] args) throws IOException
{
try{
File sourceFile=new File(args[0]);
Scanner content=new Scanner(sourceFile);
PrintWriter pwriter =new PrintWriter(args[1]);
while(content.hasNextLine())
{
String s=content.nextLine();
StringBuffer buffer = new StringBuffer(s);
buffer=buffer.reverse();
String rs=buffer.toString();
pwriter.println(rs);
}
content.close();
pwriter.close();
System.out.println("File is copied successful!");
}
catch(Exception e){
System.out.println("Something went wrong");
}
}
}
Just thought of a simple approach.
public class ReadFileReverse {
public int[] readByte(File _file) throws IOException {
FileInputStream source = new FileInputStream(_file);
int currentByte = source.available();
int readCount = 0;
int byteContainer[] = new int[currentByte];
while(readCount < currentByte){
byteContainer[readCount] = source.read();
readCount++;
}
source.close();
return byteContainer;
}
public void printReverse(int[] fileContent){
for(int byt=fileContent.length -1; byt >= 0 ; byt--){
System.out.print((char) fileContent[byt]);
}
}
public static void main(String[] args) throws IOException {
File fileToRead = new File("/README.txt");
ReadFileReverse demo = new ReadFileReverse ();
int[] readBytes = demo.readByte(fileToRead);
demo.printReverse(readBytes);
}
}
Here we are reading a file in string variable, then making a String Builder object to perform reverse operation efficiently, then printing
package com;
import java.io.FileReader;
public class Main {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("D:\\newfile.txt");
String str = "";
int ch;
//reading characters in to string variable
while ((ch = fr.read()) != -1) {
str += Character.toString((char) ch);
}
System.out.println("Original String : " + str);
//converting string variable to String Builder object
StringBuilder sb = new StringBuilder(str);
//reversing the string and printing
System.out.println("Reverse order : " + sb.reverse());
fr.close();
} catch (Exception e) {
System.out.println("error");
}
}
}
Output:

BufferReader in Java

I have a problem with BufferReader and OutputStream in Java. My aim: when you insert something from a keyboard - it goes to the file. How should I correct my code?
import java.io.*;
class IntoFile {
public static void main(String[] args) throws Exception{
try {
BufferedReader sisse = new BufferedReader(new InputStreamReader(System.in));
System.out.print ("Insert something: ");
String s = sisse.readLine();
byte[] buf = new byte[1024];
OutputStream valja = new FileOutputStream(new File(args[0]));
String line = null;
while ((line = br.readLine()) != null) {
}
valja.close();
}
catch (IOException e) {
System.out.println ("I/O: " + e);
}
}
}
Thanks!
I'd use Scanner and PrintWriter
C:\Documents and Settings\glowcoder\My Documents>javac Dmitri.java
C:\Documents and Settings\glowcoder\My Documents>java Dmitri
test
woohoo
quit
C:\Documents and Settings\glowcoder\My Documents>more out.txt
test
woohoo
quit
C:\Documents and Settings\glowcoder\My Documents>
Code:
import java.io.*;
import java.util.*;
class Dmitri {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter("out.txt");
while(in.hasNextLine()) {
String line = in.nextLine();
out.println(line);
out.flush(); // not necessary every time, but simple to do so
if(line.equalsIgnoreCase("QUIT")) break;
}
out.close();
}
}
HI I am providing a sample code through which you can write in the file after user enters characters or line from keyboard.
try{
// Create file
FileWriter fstream = new FileWriter("out.txt");
BufferedWriter out = new BufferedWriter(fstream);
out.write("Hello Java");
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
In the our.write pass the line(variable) string argument and the string will be written in that file.

Read data from a text file using Java

I need to read a text file line by line using Java. I use available() method of FileInputStream to check and loop over the file. But while reading, the loop terminates after the line before the last one. i.e., if the file has 10 lines, the loop reads only the first 9 lines.
Snippet used :
while(fis.available() > 0)
{
char c = (char)fis.read();
.....
.....
}
You should not use available(). It gives no guarantees what so ever. From the API docs of available():
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
You would probably want to use something like
try {
BufferedReader in = new BufferedReader(new FileReader("infilename"));
String str;
while ((str = in.readLine()) != null)
process(str);
in.close();
} catch (IOException e) {
}
(taken from http://www.exampledepot.com/egs/java.io/ReadLinesFromFile.html)
How about using Scanner? I think using Scanner is easier
private static void readFile(String fileName) {
try {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Read more about Java IO here
If you want to read line-by-line, use a BufferedReader. It has a readLine() method which returns the line as a String, or null if the end of the file has been reached. So you can do something like:
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line;
while ((line = reader.readLine()) != null) {
// Do something with line
}
(Note that this code doesn't handle exceptions or close the stream, etc)
String file = "/path/to/your/file.txt";
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
// Uncomment the line below if you want to skip the fist line (e.g if headers)
// line = br.readLine();
while ((line = br.readLine()) != null) {
// do something with line
}
br.close();
} catch (IOException e) {
System.out.println("ERROR: unable to read file " + file);
e.printStackTrace();
}
You can try FileUtils from org.apache.commons.io.FileUtils, try downloading jar from here
and you can use the following method:
FileUtils.readFileToString("yourFileName");
Hope it helps you..
The reason your code skipped the last line was because you put fis.available() > 0 instead of fis.available() >= 0
In Java 8 you could easily turn your text file into a List of Strings with streams by using Files.lines and collect:
private List<String> loadFile() {
URI uri = null;
try {
uri = ClassLoader.getSystemResource("example.txt").toURI();
} catch (URISyntaxException e) {
LOGGER.error("Failed to load file.", e);
}
List<String> list = null;
try (Stream<String> lines = Files.lines(Paths.get(uri))) {
list = lines.collect(Collectors.toList());
} catch (IOException e) {
LOGGER.error("Failed to load file.", e);
}
return list;
}
//The way that I read integer numbers from a file is...
import java.util.*;
import java.io.*;
public class Practice
{
public static void main(String [] args) throws IOException
{
Scanner input = new Scanner(new File("cards.txt"));
int times = input.nextInt();
for(int i = 0; i < times; i++)
{
int numbersFromFile = input.nextInt();
System.out.println(numbersFromFile);
}
}
}
Try this just a little search in Google
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());
}
}
}
Try using java.io.BufferedReader like this.
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(fileName)));
String line = null;
while ((line = br.readLine()) != null){
//Process the line
}
br.close();
Yes, buffering should be used for better performance.
Use BufferedReader OR byte[] to store your temp data.
thanks.
user scanner it should work
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
public class ReadFileUsingFileInputStream {
/**
* #param args
*/
static int ch;
public static void main(String[] args) {
File file = new File("C://text.txt");
StringBuffer stringBuffer = new StringBuffer("");
try {
FileInputStream fileInputStream = new FileInputStream(file);
try {
while((ch = fileInputStream.read())!= -1){
stringBuffer.append((char)ch);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("File contents :");
System.out.println(stringBuffer);
}
}
public class FilesStrings {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("input.txt");
InputStreamReader input = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(input);
String data;
String result = new String();
while ((data = br.readLine()) != null) {
result = result.concat(data + " ");
}
System.out.println(result);
File file = new File("Path");
FileReader reader = new FileReader(file);
while((ch=reader.read())!=-1)
{
System.out.print((char)ch);
}
This worked for me
Simple code for reading file in JAVA:
import java.io.*;
class ReadData
{
public static void main(String args[])
{
FileReader fr = new FileReader(new File("<put your file path here>"));
while(true)
{
int n=fr.read();
if(n>-1)
{
char ch=(char)fr.read();
System.out.print(ch);
}
}
}
}

Categories