Java- Issue with opening and Scanning file - java

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.

Related

FileReader not working in java and throws FileNotFoundException, although the file is in the same directory

I am trying to open a txt file in a java program and the compiler keeps advice me throwing the error, that is responsible for the program crash:
FileNotFoundException
I am working on intelliJ and the file is in the same directory I am working on, it is in the src directory.
The file is called dataset.txt and I tried all of the below formats to solve the issue.
BufferedReader br = new BufferedReader(new FileReader("D:\\Mhamed\\Courses\\Algorithms Specialization\\Course 1\\Week 2\\src\\dataset.txt"));
BufferedReader br = new BufferedReader(new FileReader("dataset.txt"));
BufferedReader br = new BufferedReader(new FileReader("/dataset.txt"));
BufferedReader br = new BufferedReader(new FileReader("./dataset.txt"));
BufferedReader br = new BufferedReader(new FileReader("src\\dataset.txt"));
BufferedReader br = new BufferedReader(new FileReader("D:/Mhamed/Courses/Algorithms Specialization/Course 1/Week 2/src/dataset.txt"));
Finally, nothing works and I don't know what is the issue. Can someone help me?
Make sure to put the file in the resources folder, that way you can use:
InputStream in = Reader.class.getClassLoader().getResourceAsStream("dataset.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in)); //read file
int lines = 0;
while (br.readLine() != null) lines++; //example to count lines
You then can apply code to every line of the file.

Read text file from inside Java eclipse executable jar

I am trying to read a text file from inside of exectutable jar. I can read it from eclipse, but can not from executable jar.I googled, and found out I have to use getClass.getResource. But all google examples are not for Buffered Reader.
My current codes are the following.
BufferedReader in = null;
try
{
File file = new File ("tfl.txt");
in = new BufferedReader(new InputStreamReader( new
FileInputStream(file),"unicode"));
...
}
Try this
InputStream localInputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("resource_name");
BufferedReader br = new BufferedReader(new InputStreamReader(localInputStream));

Read into a string (Java) [duplicate]

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.

Java read file and send in the server

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();.

Scanner reading large file

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.

Categories

Resources