Open a BufferedReader in UTF-8 - java

I have a csv file with characters like Cité, but after make the insert into the DB, I see this Cit¿
I open the file as a BufferedReader, but I don't know how to do it in UTF-8
BufferedReader br = new BufferedReader(new FileReader(csvFile));

You could explictly use a FileInputStream and an InputStreamReader using StandardCharsets.UTF_8, but it's probably simpler to use Files.newBufferedReader:
Path path = Paths.get(csvFile);
try (BufferedReader reader = Files.newBufferedReader(path)) {
// Use the reader
}
It's worth getting to know the Files class as it has a bunch of convenience methods like this.

You can use FileInputStream:
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(fileDir), "UTF8"));

Related

Getting weird characters from a web page

I am writing a program in Java that parses some text from a web page. But when I use the code below I get weird/incorrect characters.
code:
URL url = new URL(getSearchUrl(crit));
URLConnection connection = url.openConnection();
BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
I get the following output:
?}?v?8????...
So what am I doing wrong? I know that the site I want to gather info from uses utf-8.
Edit: I am currently in Crotia. I tried some other program I know worked in Serbia (my home country) but it doesn't work here.
It's g-zipped. you can see it using connection.getContentEncoding().
If you use a GZIPInputStream around the connection.getInputStream() it should work better.
BufferedReader br = new BufferedReader(
new InputStreamReader(new GZIPInputStream(connection.getInputStream()), "UTF-8"));

JCIFS: is possible get a file and use like BufferedReader?

I've a method that returns a BufferedReader, something like this:
File file = new File(fileName);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
}
catch(FileNotFoundException ex){
ex.getStackTrace();
}
How can I get a file with JCIFS API and return a BufferedReader?
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "UTF-8"));

How to get data from file which has no extension in Android?

I have a file and I want to parse data from this file. I tried but I can't get data which I want. I tried to copy/paste of my .data file but it didn't work because of some character that are included in my file.
Link of my file http://bit.ly/11meGwG
I don't know which type of file is this?
How to decode this?
InputStream is = null;
is = getResources().openRawResource(R.raw.myfile);
InputStreamReader isr = null;
isr = new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
StringBuilder sb=new StringBuilder();
String line;
try {
while((line=br.readLine())!=null){
System.out.println(line);
sb.append(line+"\n");
}
isr.close();
You are using openRawResource(). This has nothing to do with your assets/ folder. openRawResource() is for your res/raw/ folder.
Use getAssets().open() to open a file within assets/.

Substitute for Scanner when using Sockets

Is there a better substitute for Scanner class when using sockets? i am getting really tired of the .next() and .nextLine() that are really annoying to work with because they skip lines all the time.
Use InputStreamReader along with BufferedReader
Eg:
Socket s = new Socket("10.0.0.1",4444);
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
String str = new String();
while ((str = br.readLine())!=null){
System.out.println(str);
}

Convert InputStream to BufferedReader

I'm trying to read a text file line by line using InputStream from the assets directory in Android.
I want to convert the InputStream to a BufferedReader to be able to use the readLine().
I have the following code:
InputStream is;
is = myContext.getAssets().open ("file.txt");
BufferedReader br = new BufferedReader (is);
The third line drops the following error:
Multiple markers at this line
The constructor BufferedReader (InputStream) is undefinded.
What I'm trying to do in C++ would be something like:
StreamReader file;
file = File.OpenText ("file.txt");
line = file.ReadLine();
line = file.ReadLine();
...
What am I doing wrong or how should I do that? Thanks!
BufferedReader can't wrap an InputStream directly. It wraps another Reader. In this case you'd want to do something like:
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
A BufferedReader constructor takes a reader as argument, not an InputStream. You should first create a Reader from your stream, like so:
Reader reader = new InputStreamReader(is);
BufferedReader br = new BufferedReader(reader);
Preferrably, you also provide a Charset or character encoding name to the StreamReader constructor. Since a stream just provides bytes, converting these to text means the encoding must be known. If you don't specify it, the system default is assumed.
InputStream is;
InputStreamReader r = new InputStreamReader(is);
BufferedReader br = new BufferedReader(r);

Categories