BufferedReader and FileReader not working in - java

I'm just trying to read text from an existing file.txt But this program shows 2 errors
for the FileReader(file)) it says : Expected 0 arguments but found 1
and for reader.readLine() it says : Cannot resolve method 'readLine' in 'BufferedReader'
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReader {
public static void main(String[] args) {
File file = new File("fileExample.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
} catch (IOException e) {
e.printStackTrace();
}
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}

Rename your class to something other than BufferedReader, and import the right class from the JDK:
import java.io.BufferedReader;
Otherwise the compiler will look for a constructor of your own class.
Note about exception handling: given the code you have, if an IOException occurs when creating the BufferedReader, then the subsequent code will throw a NullPointerException. It may be better to just wrap the entire code in a try-with-resources block, or have the main method throws IOException.

Related

BufferedReader stuck at last input line without ending the program

I'm using BufferedReader to read data from System.in (a text file redirected context: < file.txt) then write it to the console. The problem is my program shows all lines except the last one and still works without doing any thing. If I manually end it it will write the final line.
This is my code:
public void updateText() throws IOException {
try {
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String inputLine;
while ((inputLine = bufferedReader.readLine()) != null) {
System.out.println(inputLine);
}
inputStreamReader.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Here an alternative way of waiting on available data (Ref.: Java 11 - method BufferedReader.ready()):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class TestClass {
public static void main(String[] args) {
try (InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
String line;
// wait until data available
while (!bufferedReader.ready()){
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Example output:
Hello world
Hello world
Hello world
Hello world
If this only occurs in Eclipse, your issue is most likely a duplicate and bug 513713.
You don't need to read standard input line by line when you could simply transfer the data in one step, replacing the body of updateText():
System.in.transferTo(System.out);

Exception error does not happen and try ... catch is not printing specified error message

I have the following code which attempts to run and print a specified error message when the file is not found. However, it's running perfectly without giving output about the error that happens. the text file name does not exist in my directory. Can anyone shed some light on this?
When I run this it runs perfectly without any error (not sure if that's a good thing).
import java.io.*;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.IOException;
public class filereader {
public static void main (String[] args) {
String fileName = "nonExistenceFileName.csv";
File textFile = new File(fileName);
try (BufferedWriter br = new BufferedWriter(new FileWriter(textFile))) {
String line;
Scanner input = new Scanner(textFile);
while(input.hasNextLine()) {
line = input.nextLine();
System.out.println(line);
}
} catch (FileNotFoundException e) {
System.out.println("Can't find file " + textFile.toString());
} catch (IOException e) {
System.out.println("Unable to read file");
}
When you try to create a BufferWriter object on a non-existing file
BufferedWriter br = new BufferedWriter(new FileWriter(textFile));
it creates a new file (if not already exists)
So no exception is thrown since the file is already present
you should check if the file exist before, otherwise, BufferWriter creates one if it doesn't exist. use
if (textFile.exists()){
//what to do if file exist
}else{
//Throw FileNotFound Error
}

Reading in Data (Java)

I am having trouble loading data into my Java program. The program says the file exists, but FileReader is giving a FileNotFoundException. I have tried the full path and made sure all files are closed. I am using the Eclipse IDE. Any suggestions?
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
public class ReadCSV {
public static void main(String[] args) {
String filepath = "/Users/mrodgers/Documents/other/languages/java/eclipse/ReadCSV/src/data.csv";
//String filepath = "~/Desktop/data.csv";
//String filepath = "data.csv";
File mjr = new File(filepath);
System.out.println(mjr.exists());
FileReader fr = new FileReader(mjr);
// BufferedReader br = new BufferedReader(fr);
}
}
This works on my machine, with the filename set to one that I have of course.
But I did have to wrap the creation of FileReader in a try/catch for FileNotFound exception -- is that what you mean? The compiler tells you that you must catch FileNotFound if you use that FileReader constructor?
Unless/until you do that, eclipse should (and does, on my machine) mark the call to the FileReader constructor as an error because that exception is not caught.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class FilePlay
{
public static void main(String[] args) {
String filepath = "/Users/ralph/Documents/My Chess Database/r-b.pgn";
//String filepath = "~/Desktop/data.csv";
//String filepath = "data.csv";
File mjr = new File(filepath);
System.out.println(mjr.exists());
try
{
FileReader fr = new FileReader(mjr);
BufferedReader br = new BufferedReader(fr);
}
catch (FileNotFoundException fnf)
{
fnf.printStackTrace();
}
}
}

Data missing during importing data from source to sink file in kafka

I have been using kafka to get and process streaming inputs and I have the source and sink properties as:
name=local-file-sink
connector.class=org.apache.kafka.connect.file.FileStreamSinkConnector
tasks.max=10
file=pnrsink5.xml
topics=test
name=local-file-source
connector.class=org.apache.kafka.connect.file.FileStreamSourceConnector
tasks.max=10
file=pnrtes.xml
topic=test
And I run the standalone as:
bin/connect-standalone.sh config/connect-standalone.properties config/connect-file-source.properties config/connect-file-sink.properties
And when I give parsed input using a java program written as:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class parse
{
public static void main(String args[]) throws IOException
{
int count=0,c=1,a=0;
String file="/home/tthteg/speed_pnr/Source/Source_pnr.xml";
String l1="</PNR>";
String line;
try
{
FileReader fileReader=new FileReader(file);
BufferedReader bufferedReader=new BufferedReader(fileReader);
String file="/home/tthteg/speed_pnr/Source/Source_pnr.xml";
String l1="</PNR>";
String line;
try
{
FileReader fileReader=new FileReader(file);
BufferedReader bufferedReader=new BufferedReader(fileReader);
BufferedWriter bufWriter = new BufferedWriter(new FileWriter("/home/tthteg/speed_pnr/kafka/pnrtes.xml"));
try
{
while((line=bufferedReader.readLine())!=null)
{
if(c%2==0)
{
Thread.sleep(5000);
c=1;
}
if(line.contains("<PNR"))
{
c++;
System.out.println(line);
bufWriter.write(line);
while((line=bufferedReader.readLine()).equals(l1)==false)
{
System.out.println(line);
bufWriter.write(line);
}
bufWriter.write("</PNR>");
bufWriter.flush();
System.out.println("</PNR>");
count++;
a=count;
}
}
}catch(Exception e)
{
System.out.println(e);
}
bufferedReader.close();
bufWriter.flush();
System.out.println(a);
}catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(a);
}
}
PROBLEMS:
1.Whenever I run the java file,I have to touch the source file(echo -e >> pnrtes.xml) to get the data imported to the sink file(pnrsink5.xml)
2.I have found some data missing at the beginning(some 4 words) in the sink file.
Thank you in advance

why do i get the error "The system cannot find the file specified?" [duplicate]

This question already has answers here:
java.io.FileNotFoundException: the system cannot find the file specified
(8 answers)
Closed 7 years ago.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class FileRead
{
public static void main (String[] args) throws IOException
{
try
{
FileInputStream fstream = new FileInputStream("data.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null)
{
System.out.println (strLine);
}
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
I put the data.txt in the same location as the java project. I have to get this done for a research project for my java class.
Use src/data.txt to find a file in the same place as your project :)
Means that it is the same location as your source folder (put it inside src)
you forgot ./ thats why it says not found file

Categories

Resources