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));
Related
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();
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 am trying to download contents from ftp folder. There is one xml file which starts with standardazed xml codes.
< ?xml version="1.0" encoding="utf-8"?>
when i read these files (using java.net.Socket)and get input stream and then try to convert to String, somehow i get some new charecters. And the whole xml document starts with '?' eg. "?< ?xml version="1.0" encoding="utf-8"?>....."
BufferedInputStream reader = new BufferedInputStream(sock.getInputStream());
Then i am getting a string from this reader using following code.
StringBuilder sb = new StringBuilder();
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(reader));
while ((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println ("sb.toString()");
Not sure whats happening here. why am i getting some special charecters introduced ?Any suggestions would be appreciated
and then i just used following code to read the file and in console i see some special charecters
BufferedReader reader = new BufferedReader(new FileReader("c:/Users/appd922/DocumentMeta06122014.xml"));
StringBuffer sb = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String output = sb.toString();
System.out.println("reading from file"+output);
I got output starting
"reading from file< ?xml version.....
where am i getting these special charecters ?
Note- ignore the space in the xml file line given above. i could not write here with proper xmlwithout that space.
Specify the encoding when creating InputStreamReader to read the file from the ftp, for example:
BufferedReader br = new BufferedReader(new InputStreamReader(reader, "utf-8"));
Otherwise, InputStreamReader uses default encoding. Also, specify the encoding when reading the downloaded file. FileReader uses default platform encoding. Use InputStreamReader and specify encoding, for example:
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8"));
Those characters are called BOM, Byte Order Mark. If you set the encoding of the InputStreamReader to 'UTF-8', you could see that they are interpreted as a single character, that is the BOM character.
Unfortunately, you have to handle this character yourself, because Java won't do it for you: java utf-8 and bom. Usually you just strip your stream of it. Good luck.
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());