I'm currently working with BufferedReader, and was wondering, When we create a new BufferedReader with an InputStreamReader (i.e new BufferedReader(new InputStreamReader(FileInputStream)) as the parameter for it, does that mean the BufferedReader is already reading in the entire bytes of a file from the FileInputStream via InputStreamReader? Or is it only reading the necessary bytes as it's controlled to (i.e readLine()).
Below is an example of my code:
File inputFile = new File("C:\\foo\\foo.txt");
FileInputStream fis = new FileInputStream(inputFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line = reader.readLine();
Related
I am trying to read a text file from a specified path but have this error The System cannot find the file specified
This is the code:
File file=new File("D:\\Progs\\FinalCS\\Records\\record.txt");
FileReader FileR=new FileReader(file);
Scanner scan=new Scanner(FileR);
String uri = "d:\\123.txt"
FileInputStream fis = new FileInputStream(uri);
InputStreamReader isr =new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String s;
while((s=br.readLine())!=null){
// read line
}
We can wrap an InputStream and turn it into a Reader by using the InputStreamReader class in java:-
Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
Is there any benefit of doing this when I already have the Reader available?
From Input Stream i am reading the image data and convert it to string. From string am writing to an image directly by following type.
final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
final char[] cbuf = new char[1024];
final int length = reader.read(cbuf);
String packet=new String(cbuf,0,length);
BufferedWriter out = null ;
FileWriter fstream ;
File file = new File(fileName);
fstream = new FileWriter(file);
out.write(packet);
Please guide me in this issue.
I am not getting full image.
final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
Decodes input using default encoding potentially corrupting data.
out.write(packet);
Encodes characters using default encoding potentially corrupting data.
Read documentation on API you use. Only perform conversion with default or unknown encoding when you absolutely need it.
Read/convert an InputStream to a String
I want to convert InputStream into CSVReader. When I am using the code below
CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
it is working fine, but, if I fetch the file from class path, then this code is not working.
How do I convert a InputStream to CSVReader?
This is not working cause InputStream is not a string type:
InputStream input = ImportCsv.class.getResourceAsStream("/dataUpload/staff.csv");
CSVReader reader = new CSVReader(new FileReader(input));
Use
CSVReader reader = new CSVReader(new InputStreamReader(input));
Edit to answer the comment
The problem is because you don't have any constructor for FileReader as you want. See the FileReader API for details. But since you will have to convert the byte stream to character stream so you will have to use InputStreamReader. For better performance you can wrap it with a BufferedReader also
CSVReader reader = new CSVReader(new InputStreamReader(input, "UTF-8"));
The bridge between binary bytes / InputStream, and unicode text (Reader/String) is the InputStreamReader. Specify the encoding of the bytes / InputStream if the file is not local.
I have a variable, inFileName of type JFileChooser.
I've called this variable to the method HexFinder in class checksumFinder. It is to be used in the inputStreamReader inside a BufferedReader. (I'm using this line to call it)
cf.HexFinder(inFileName,null,null,null);
This is causing an error as the inputStreamReader will only accept variables of type String. (Here's my code for the BufferedReader)
BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(inFileName)));
Is there a way that I can get the inputStreamReader to read in inFileName? If not then how can I resolve this?
Any help is much appreciated.
If you are trying to read a file chosen by a JFileChooser then you can do the following;
File file = inFileName.getSelectedFile();
BufferedReader reader = new BufferedReader(new FileReader(file));
Note that FileReader uses the default character encoding. You can manually specify the encoding like this;
String charset = "UTF-8";
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));