I have a csv file which contains words in english followed by their Hindi translation. I am trying to read the csv file and do some further processing with it. The csv file looks like so:
English,,Hindi,,,
,,,,,
Cat,,बिल्ली,,,
Rat,,चूहा,,,
abandon,,छोड़ देना,त्याग देना,लापरवाही की स्वतन्त्रता,जाने देना
I am trying to read the csv file line by line and display what has been written. The code snippet (Java) is as follows:
//Step 2. Read csv file and get the string.
FileInputStream fis = null;
BufferedReader br = null;
try {
fis = new FileInputStream(new File(csvFile));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
boolean startSeen = true;
if(fis != null) {
try {
br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
System.out.print("Unsupported encoding");
}
String line = null;
if(br != null) {
try {
while((line = br.readLine()) != null) {
if(line.contains("English") == true) {
startSeen = true;
}
if((startSeen == true) && (line != null)) {
StringBuffer sbuf = new StringBuffer();
//Step 3. Parse the line.
sbuf.append(line);
System.out.println(sbuf.toString());
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
However, the following output is what I get:
English,,Hindi,,,
,,,,,
Cat,,??????,,,
Rat,,????,,,
abandon,,???? ????,????? ????,???????? ?? ???????????,???? ????
My Java is not that great and though I have gone through a number of posts on SO, I need more help in figuring out the exact cause of this problem.
For reading text file it is better to use character stream e.g by using java.util.Scanner directly instead of FileInputStream. About encoding you have to make sure first that the text file that you want to read is saved as 'UTF-8' and not otherwise. I also notice in my system, I have to save my java source file as 'UTF-8' as well to make it shown hindi char properly.
However I want to suggest simpler way to read csv file as follow:
Scanner scan = new Scanner(new File(csvFile));
while(scan.hasNext()){
System.out.println(scan.nextLine());
}
I think your console cannot show Hindi chars. Try
System.out.println("Cat,,बिल्ली,,,");
to test
So as discussed in above answers; solutions it is TWO steps
1) Save your txt file as UTF-8
2) Change the property of your Java code to use UTF-8
In Eclipse; right click on Java file;
Properties -> Resurces -> Text File Encoding -> Other -> UTF-8
Refer screenshot given on
http://howtodoinjava.com/2012/11/27/how-to-compile-and-run-java-program-written-in-another-language/
Related
I need to read the contents of a hosted CSV file using java. Hitting this URL where the CSV is hosted downloads the file into the browser. How do I access this file and read its contents without having to do anything locally?
Currently I have:
try {
URL url = new URL("URL here");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
CsvReader products = new CsvReader(in);
products.readHeaders();
while (products.readRecord()) {
products.get("ID"));
}
}
products.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I expect the products.get("ID")) to retrieve data from the ID column instead I get a string containing symbols and gibberish.
Does anyone have any ideas on how I may achieve this?
Thanks in advance!
Two main points to be noted here
BufferedReader is a class in Java that reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, lines, and arrays.REFER HERE
each row in BufferedReader is a String Input stream you need to separate the string with separator usually a ',' for CSV files
Try like this hope this is what you are trying to accomplish here.
try {
URL url = new URL("URL here");
URLConnection urlConn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
((URLConnection) urlConn).getInputStream()));
String row;
while ((row = in.readLine()) != null) {
String[] values = row.split(","); // separator
System.out.println("Product ID= " +values[0]); // change 0 to the column index of the file
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Thanks,
Happy Coding <3 !
I have the following problem. I have create a method that read a very big textual file starting by its path.
The file is read line by line and each line is appended into a StringBuffer.
This is my code:
public void run(Vector parametri) {
if (parametri != null && (parametri.isEmpty() == false)) {
gvParam = (Vector) parametri.clone();
} else {
TraceLog.scrivi("Test Esistenza Parametri", "Parametri mancanti", false, TraceLog.lowConsole + TraceLog.highTrace + TraceLog.highLog);
target.azione("Parametri mancanti !!");
return;
}
String fattureXml = gvParam.get(0).toString();
// READ THE FILE:
StringBuffer fileContent = new StringBuffer();
try {
// Creazione del reader per leggere il file:
BufferedReader br = new BufferedReader(new FileReader(fattureXml));
String line = null;
while ((line = br.readLine()) != null) {
//System.out.println(line);
fileContent.append(line);
}
System.out.println(fileContent.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The reading section begin after the comment // READ THE FILE:
The file read seems work fine because I used this line (actually commented)
System.out.println(line);
to check the readed line and it read it correctly.
As you can see now I am adding this line to a StringBuffer (to obtain a big String representing the content of the readed file), by:
fileContent.append(line);
The problem is that, after the loop, I am trying to print in the console the content of the fileContent StringBuffer, by:
System.out.println(fileContent.toString());
but nothing is printed.
Why? What am I missing? What exactly contains the fileContent StringBuffer? Is it something like a big String containing the lines of the readed file or what?
Here's my guess: Since you're discarding all new-line characters, you end up with a single enormously long line, which your console is having troubles handling. (I've experienced this myself in the Eclipse console.)
Try changing
fileContent.append(line);
to
fileContent.append(line).append('\n');
As #assylias points out you also might want to drop Vector (and use for instance an ArrayList instead). Also, have a look at this question: Difference between StringBuilder and StringBuffer.
For future reference, the "modern" way of doing this would be
String content = new String(Files.readAllBytes(Paths.get(fileName)));
I am trying to read a text file in java, but I am having this annoying FILENOTFOUND EXCEPTION:
try {
BufferedReader br = new BufferedReader (new FileReader("/home/asad/workspace/MyFirstProject/HelloWorld.txt"));
while ((reader = br.readLine())!= null);
System.out.print(reader);
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This is the simplest code and should read a text file name HelloWorld from the specified path, should store it in the string "reader" and displays its output on the console, but the compiler is having problems locating the file.
Strange thing is, I have tried looking for the file path using getCanonicalPath() and it is specifying to the same path I have been looking, but the compiler is not finding the file. Any help?
Try to check this test before your code:
File file = new File("/home/asad/workspace/MyFirstProject/HelloWorld.txt");
if( file.exists() ) {
System.out.println("File exists!");
} else {
System.out.println("File not existst!");
}
if you get FileNotFoundException, file isn't in this localization or file name is incorrect.
i just spotted the problem of my code,
by omitting the .txt from the path printed the solution.
i changed the code from
BufferedReader br = new BufferedReader (new FileReader("/home/asad/workspace/MyFirstProject/HelloWorld.txt"));
to
BufferedReader br = new BufferedReader (new FileReader("/home/asad/workspace/MyFirstProject/HelloWorld"));
and boosh the console printed the text stored in the file.
In the while-loop the empty statement ; was executed till the end of file.
if ((reader = br.readLine())!= null) { // Not ;
System.out.println(reader); // Not print, as reader has "\r\n" removed
}
br.close();
Using if instead of the usual while to retrieve the first line only.
The FileNotFoundException is probably true. To find the typo you can do in a terminal:
cat /home/asad/workspace/MyFirstProject/HelloWorld.txt
ls /home/asad/workspace/MyFirstProject
ls /home/asad/workspace
Most likely it should have been "MyfirstProject" or so.
Is there a way to check whether a file was correctly written, I mean if there is an EOF at the end?
I'm asking that because I have a program that takes some file, merge them in a very big file and then use it to get statistics from it.
The point is that the second part never ends because it doesn't recognize the end of file.
The relevant parts of the code are the following:
(please do not ask for the whole code as I cannot post for important reasons)
FileWriter file=null;
PrintWriter pw = null;
String pathToRead=null;
InputStreamReader isr = null;
BufferedReader br = null ;
FileInputStream fis = null ;
TestJFileChooser d=new TestJFileChooser();
int c=1;
String line=null;
....
//here i select the files
selectedFile=new File(pathToRead);
//here I get one buffer reader for each file got with listFiles()
for(File file_sel:app){
if (file_sel.getName().startsWith("gtou")){
System.out.println(file_sel.getName());
fis = null;
try {
fis = new FileInputStream(file_sel);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
isr=new InputStreamReader(fis);
br=new BufferedReader(isr);
map.put(i, br);
num_file++;
i++;
}
}
//then I select the output file and open a print writer for it
fileToWrite=new File(pathToRead);
try {
file = new FileWriter(fileToWrite);
pw= new PrintWriter(file);
} catch (IOException e1) {
e1.printStackTrace();
}
//merging part
....
line=br.readLine();
while(line!=null){
System.out.println("line is:"+line);
....
line=br.readLine();
}
//end of merging ....
pw.flush();
pw.close();
try {
if (file!=null) file.close();
fis.close();
isr.close();
br.close();
for(int fi=0;fi<num_file;fi++){
br2=map.get(fi);
br2.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
so.kill();
Runtime r=Runtime.getRuntime();
r.gc();
//this is a popup that comes out
GlitchSquad gli=new GlitchSquad("Completed");
the problem is that as output I get:
line is: null ;
line is: null ;
line is: null ;
etc
And never get to "completed" popup =(
I cannot understand what is exactly that null because the control line!=null doesn't work.
I also tried to use that null as a string ..but nothing..
I thought that was a problem in how I close the streams but now the code seems correct to me ..but still no way to stop it..
Suggestion?
Thanks in advance!
p.s. it is a summarized version in order to focus on the streams.. variables are correctly declared and the same is for imports etc
edit: code updated
EOF is EOF. There is no more data. Unless you have an expected EOF mark within the file, or a self-describing protocol that tells you where the EOF mark should be, there is no way to determine whether the file was completely written.
I don't know if it will solve your problem, but I'd be using this code instead:
try {
fis = new FileInputStream(file_sel);
isr=new InputStreamReader(fis);
br=new BufferedReader(isr);
map.put(num_file++, br);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Otherwise there may be uncaught "NullPointer"-exceptions or strange BufferedReaders in your "map". ( I don't right now know how new InputStreamReader(null) will behave.)
It looks like i and num_file have always equal values, so just drop i. Or use a LinkedList and drop both.
If there's not a special merging that you have to do, I'd just do it like this:
OutputStream os;
try {
os = new FileOuputStream(outfile);
} catch (FileNotFoundException e) {
os = null;
e.printStackTrace();
}
if (os != null) {
for(File file_sel:app) {
if (file_sel.getName().startsWith("gtou")) {
System.out.println(file_sel.getName());
InputStream is = null;
try {
is = new FileInputStream(file_sel);
byte[] buffer = new byte[1024];
int readBytes = 0;
while ((readBytes = is.read(buffer)) > 0) {
os.write(buffer, 0, readBytes);
}
fos.flush();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
If you read files with different encodings, you will have to modify at least the reading of course.
If it doesn't work, I'd suggest you build a "summarized" and runable sample program.
The core of your question is this code:
BufferedReader br = ...
String line = br.readLine();
while (line != null) {
System.out.println("line is:" + line);
...
line = br.readLine();
}
You say that this repeatedly outputs this:
line is: null ;
line is: null ;
(Notice the " ;" on the end!!!)
The only way that can happen is if the file you are reading contains at least one line that look like this:
null ;
Indeed, unless the "..." code includes a continue statement, there must must be lots of those lines in the input file.
Is there a way to check whether a file was correctly written?
Yea. Look at it using a text editor and/or check its file size.
I mean if there is an EOF at the end?
In modern file systems, EOF is a position not a marker. Specifically it is the position after the last byte of the file. So it is logically impossible for a file to not have an EOF. (You'd have to have a file that is infinite in length for there to be no EOF.)
I am trying to write a new line to a text file in android.
Here is my code:
FileOutputStream fOut;
try {
String newline = "\r\n";
fOut = openFileOutput("cache.txt", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.write(newline);
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I have tried \n, \r\n and I did also try to get the system property for a new line, neither of them work.
The data variable contains previously data from the same file.
String data = "";
try {
FileInputStream in = openFileInput("cache.txt");
StringBuffer inLine = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in, "ISO8859-1");
BufferedReader inRd = new BufferedReader(isr,8 * 1024);
String text;
while ((text = inRd.readLine()) != null) {
inLine.append(text);
}
in.close();
data = inLine.toString();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I had the same problems, tried every trick in the book.
My problem: the newline's were written, but while reading they were removed:
while (readString != null) {
datax.append(readString);
readString = buffreader.readLine();
}
The file was read line by line and concatenated, so the newline's disappeared.
I did not look at the original file in Notepad or something because I didn't know where to look on my phone, and my logscreen used the code which removed the newline's :-(
So the simple soultion was to put it back while reading:
while (readString != null) {
datax.append(readString);
datax.append("\n");
readString = buffreader.readLine();
}
I executed a similar program and it worked for me. I observed a strange behavior though. It added those new lines to the file, however the cursor remained at the first line. If you want to verify, write a String after your newline characters, you will see that the String is written just below those new lines.
I was having the same problem and was unable to write a newline. Instead I use BufferdWritter to write a new line into the file and it works for me.
Here is a sample code sniplet:
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("cache.txt",0));
BufferedWriter bwriter = new BufferedWriter(out);
// write the contents to the file
bwriter.write("Input String"); //Enter the string here
bwriter.newLine();