How do I feed an InputStream into a CSVReader? - java

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.

Related

When to wrap the InputStream in Java rather than directly using the Reader in case of characters?

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?

BufferedReader - Does it take in the entire document?

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

Unable to recover a full image using java bufferwriter?

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

inputStreamReader won't recognise type JFileChooser

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

How make InputStreamReader fail on invalid data for encoding?

I have some bytes which should be UTF-8 encoded, but which may contain a text is ISO8859-1 encoding, if the user somehow didn't manage to use his text editor the right way.
I read the file with an InputStreamReader:
InputStreamReader reader = new InputStreamReader(
new FileInputStream(file), Charset.forName("UTF-8"));
But every time the user uses umlauts like "รค", which are invalid UTF-8 when stored in ISO8859-1 the InputStreamReader does not complain but adds placeholder characters.
Is there is simple way to make this throw an Exception on invalid input?
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
InputStreamReader reader = new InputStreamReader(
new FileInputStream(file), decoder);
Simply add .newDecoder():
InputStreamReader reader = new InputStreamReader(
new FileInputStream(file), Charset.forName("UTF-8").newDecoder());

Categories

Resources