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);
Related
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"));
what the difference between both stream chaining methods ?
first :
BufferedReader br = new BufferedReader(
new InputStreamReader(someUrlConnection.getInputStream(), encoding));
second :
InputStream raw = someUrlConnection.getInputStream();
InputStream buffer = new BufferedInputStream(raw);
Reader reader = new InputStreamReader(buffer);
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"));
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/.
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);
}