character encoding issue in converting csv data using fileInputStream class in java - java

Hi am having a csv file in my hand and am trying to read each line in csv file and update the content to a database table. Am doing this using java.
Following are the things i did for achieving this.
FileInputStream fileInputStream = FileUtils.openInputStream("filename.csv");
dataInputStream = new DataInputStream(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
String strLine;
while((strLine = bufferedReader.readLine()) != null){
System.out.println(strLine);
}
but its printing something like blocks on the console, not the actual data csv having
can anyone please help me to solve this issue.?

you should find out what encoding it is and then declare it while reading, xample for UTF-16:
new InputStreamReader(zipFile.getInputStream(entry), "UTF_16" )

Related

How to read a file from internal storage?

I've just created a file in MainActivity using the code:
FileOutputStream outputStream = openFileOutput("user", Context.MODE_PRIVATE);
outputStream.close();
Now what to use if I want to read the file (noting that the file will be obviously created in the internal storage)?
And is there a way that makes the file reading works as the Scanner function in Java (where the string is being read word by word and line by line)?
Use openFileInput() with the same parameters as openFileOutput().
This is your solution what I understood from your question,
FileInputStream fIn = new FileInputStream(new File("FILE_PATH"));
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(fIn));
String str = bufferedReader.readLine();

Storing values to be retrieved by user

I'm working on a program that allows the user to input and view passwords that are encrypted when inputted and decrypted when accessed by the user. I figured that this could be done using a text document, however I'm unsure of how to get my code to interact with said document. Any ideas on how to do this/alternative ways that I can accomplish my task?
You could save your input in a text file with BufferedWriter and read the document with BufferedReader.
An example would be:
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(password)
out.close
BufferedReader in = new BufferedReader(new FileReader(file));
while((line = in.readLine()) != null){
System.out.println(line);
}
in.close

Regarding word search error (Encoding error Java)

I have a list of french words where I am trying to search in my database. The words are "thé Mariage frères", "thé Lipton" etc.
While I am reading my file in java, it shows the words as "thé Lipton", "thé Mariage frères". It fails to get the correct words.
I don't know how to correct my errors.
Help me, please!!!
You file is in one encoding (maybe latin1/iso-8859-1) and you're reading your file in another encoding.
See if this port helps How to read a file in Java with specific character encoding?
Try this.
try (FileInputStream fis = new FileInputStream("input.txt");
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr)) {
String line;
while ((line = reader.readLine()) != null)
System.out.println(line);
}
Try creating Scanner object like this
Scanner s = new Scanner(new File("French_Tea_keywords/filter_keywords.txt"), "UTF8");

Junk characters while reading text file in java

I have a java which calls windows bat file which does some processing and generates the output file.
Process p = Runtime.getRuntime().exec("cmd /c "+filename);
Now when reading the file from following program. (filexists() is function which checks whether file exists or not). Output file contains only single line
if ( filexists("output.txt") == true)
{ String FileLine;
FileInputStream fstream = new FileInputStream("output.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
FileLine = br.readLine();
fstream.close();
filein.close();
}
Variable FileLine contains 3 junk charcters in the starting. I also checked few other files in the progam and no file has this issue except for the fact it is created with Runtime function.
9087.
As you can see three junk characters are coming in the output file. When opened with Notepad++, i am not able to see those junk characters.
Please suggest
This is happening because you have not mentioned the file encoding while creating your FileInputStream.Assuming your file is UTF-8 encoded, you need to do something like this
new FileInputStream("output.txt, "UTF-8"));
Change the encoding as per the encoding of your file
That looks like the byte order mark for UTF-8 encoding. See https://en.wikipedia.org/wiki/Byte_order_mark
May be its an issue with file encoding. Though I am not sure.
Can you please try following piece of code and see if it works for you
BufferedReader in = new BufferedReader(
new InputStreamReader( new FileInputStream("output.txt"), "UTF8"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}

Reading hebrew from text file with Java

I'm having troubles with reading a UTF-8 encoded text file in Hebrew.
I read all Hebrew characters successfully, except to two letters = 'מ' and 'א'.
Here is how I read it:
FileInputStream fstream = new FileInputStream(SCHOOLS_LIST_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
if(strLine.contains("zevel")) {
continue;
}
schools.add(getSchoolFromLine(strLine));
}
Any idea?
Thanks,
Tomer
You're using InputStreamReader without specifying the encoding, so it's using the default for your platform - which may well not be UTF-8.
Try:
new InputStreamReader(in, "UTF-8")
Note that it's not obvious why you're using DataInputStream here... just create an InputStreamReader around the FileInputStream.

Categories