I am playing around with the Scanner class for learning purposes and i use it to read a very large file (60.000 lines aprox) without using the Reader class , and it stops reading after approximately 400 lines. Do i have to use a Bufferedreader inside the Scanner's constructor or the problem is something else? I want to know why this is happening. Thanks.
My code is the usual code to output all the lines.
File file1 = new File("file1");
Scanner in= new Scanner(file1);
while (scan.hasNextLine() ) {
String str = scan.nextLine();
System.out.println(str);
}
This issue is usually more common on 64 bit machines or with files having size more than 1-2 GB and does not have anything to do with heap space. Switch to BufferedReader it should work fine,
BufferedReader br = new BufferedReader(new FileReader(filepath));
String line = "";
while((line=br.readLine())!=null)
{
// do something
}
I just experienced this very problem. It seems that it works just by changing the scanner construction. Replace this:
File file1 = new File("file1");
Scanner in= new Scanner(file1);
with this:
FileReader file1 = new FileReader("file1");
Scanner in= new Scanner(file1);
Maybe the problem appears when you build your scanner from a file without the system knowing that it is a text file.
Related
I am trying to read a log file word by word using a scanner and using the code
Scanner scanner = new Scanner(file);
while(scanner.hasNext()){
String word = scanner.next();
}
But the problem is that this stops after it reaches the end of the file but I need to read it as it gets generated
I tried this to solve this using the below code
Scanner scanner = new Scanner(file);
while(true){
while(!scanner.hasNext()){
Thread.sleep(1000);
}
String word = scanner.next();
}
But the code does not seem to work and gets stuck in the while loop even when the log file has more data appended to it.
Can someone point out what i am doing wrong.
I made an small snippet for reading a log file for another system. I used BufferedReader instead of Scanner. Because it will run until the end of file and still reading. Instead of using the Scanner Next.
readLine() Doc:
* #return A String containing the contents of the line, not including
* any line-termination characters, or null if the end of the
* stream has been reached
And My Snippet
BufferedReader br = new BufferedReader(new InputStreamReader(file));
while (true)
{
strLine = br.readLine();
if(strLine!=null)
{
System.out.println(strLine);
}else{
Thread.sleep(100);
}
}
I am playing around with the Scanner class for learning purposes and i use it to read a very large file (60.000 lines aprox) without using the Reader class , and it stops reading after approximately 400 lines. Do i have to use a Bufferedreader inside the Scanner's constructor or the problem is something else? I want to know why this is happening. Thanks.
My code is the usual code to output all the lines.
File file1 = new File("file1");
Scanner in= new Scanner(file1);
while (scan.hasNextLine() ) {
String str = scan.nextLine();
System.out.println(str);
}
This issue is usually more common on 64 bit machines or with files having size more than 1-2 GB and does not have anything to do with heap space. Switch to BufferedReader it should work fine,
BufferedReader br = new BufferedReader(new FileReader(filepath));
String line = "";
while((line=br.readLine())!=null)
{
// do something
}
I just experienced this very problem. It seems that it works just by changing the scanner construction. Replace this:
File file1 = new File("file1");
Scanner in= new Scanner(file1);
with this:
FileReader file1 = new FileReader("file1");
Scanner in= new Scanner(file1);
Maybe the problem appears when you build your scanner from a file without the system knowing that it is a text file.
I need to read contents of a file as a server, and then send the read data file, for the client so the client print it out on the Client terminal.
The problem is that I can't find a way or method to read a txt file from the current directory which my java file and txt file are existed.
Please help me.
There are many ways to read text file or file in java. It depend on you to that in which format you need to pass your file content to client side.
Here are some method to reading file in java.
1. Using BufferedReader class
BufferedReader input = new BufferedReader(new FileReader(aFile));
String line = null; //not declared within while loop
while (( line = input.readLine()) != null){
String curLine = line;
//Process line
}
2.Using Apache Common IOUtils with the class IOUtils.toString() method.
FileInputStream inputStream = new FileInputStream("FILEPATH/FILENAME");
try {
String everything = IOUtils.toString(inputStream);
} finally {
inputStream.close();
}
3.Using the Scanner class in Java and the FileReader
Scanner in = new Scanner(new FileReader("FILENAME/FILEPATH"));
while (scanner.hasNextLine()){
//process each line in some way
String line = scanner.nextLine();
}
Scanner has several methods for reading in strings, numbers, etc...
4.In JAVA 7 this is the best way to simply read a textfile
new String(Files.readAllBytes(...))
or Files.readAllLines(...)
Path path = Paths.get("FILENAME");
List<String> allLines = Files.readAllLines(path, ENCODING);
Please refer this link for more onfomation.
You can use BufferedReader to read from a txt file.
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
here fileName is a string that contain your absolute file name.
eg : fileName = "C:\temp\test.txt";
You can read file by using BufferedReader.
File file=new File("filepath");
BufferedReader br=new BufferedReader(new FileReader(file)); //Here you create an object of bufferedreader which file read through filereader
String data=br.readLine();
while(data!=null)
{
System.out.println(data); // Writing in the console
data=br.readLine();
}
This will taking input from file and giving output to console.If you want it write in other file then use BufferedWriter.
File out=new File("outputfilepath");
BufferedWriter bw=new BufferedWriter(new FileWriter(out));
simply us bw.write() instead of System.out.println();.
I am writing a program in Java, it scans a file, counts lines, character, palindromes, words. My problem is when I ask for a filename, I am using BufferedReader and InputStreamReader to scan the file the user provided, and print the results in another file, my program compiles, when I type in the name of the file nothing happens, program does not finish, and remains stuck, here is code the BufferedReader, if the entire code is needed i will post it up
System.out.println("Enter the name of the file you would like to scan: ");
String fileName = scan.nextLine();
File file = new File(fileName);
BufferedReader br = new BufferedReader(newInputStreamReader(System.in));
Scanner scanner = new Scanner(System.in);
String fileName = scanner.next();
scanner.nextLine();
FileReader file = new FileReader(fileName);
BufferedReader br = new BufferedReader(FileReader);
Try with
BufferedReader br = new BufferedReader(new FileReader(fileName));
actually you are specifying your reader InputStreamReader to read from System as System.in though you are trying to read a file. So you have to use FileReader. See How to read file in Java
also thanks to #user1009560 you can use
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
You're creating an InputStreamReader object as System.in as the inputStream property. You'll need to specify a FileInputStream for as the InputStream.
During one of my assignment i was trying to read a file and print it line by line. I tried to use the hasNextLine again and it didn't work. I tried to complete it somehow but i want to know if there is some way i can go through my file all again without creating a new Scanner.
here is my code snippet
sc = new Scanner(new File(args[0]));
while (sc.hasNextLine()) {
System.out.println(sc.nextLine());
}
I want to use the while again later but i cant do that. Is there a way to reset or bring it back to the top and traverse through the file again.
You can just create a new Scanner after closing the first one:
sc.close();
sc = new Scanner(new File(args[0]));
// do the same thing again
To optimize, you can save reference to file for future use.
File f = new File(args[0]);
sc = new Scanner(f);
//...
sc = new Scanner(f);
Scanner is anything but smart iterator, so cost of creating new can be smaller than 'rewinding' old one.
You can use a FileReader decorating a RandomAccessFile:
RandomAccessFile f = new RandomAccessFile(args[0], "r");
Scanner sc = new Scanner(new FileReader(f.getFD()));
//Read through scanner
//Rewind file
f.seek(0L);
//Read through file again