With that code
BufferedReader reader;
InputStream fis;
fis = Logs.class.getClassLoader().getResourceAsStream("data");
reader = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
I can read from data file from src folder.
How can I read it from config folder?
And main question How can I write there?
Btw
getResourceAsStream("../config/data");
is not working, if I want to read from config directory.
Related
I am downloading a Google Cloud Storage object (a GZIP file of size ~400MB, 25 million rows records), with the following code:
Blob blob = storage.get(bucketName, blobName);
ReadChannel readChannel = blob.reader();
The ReadChannel is been gunzip and passed to BufferedReader with following:
BufferedReader reader =
new BufferedReader(
new InputStreamReader(
new GZIPInputStream(
Channels.newInputStream(readChannel)),
UTF_8));
Problem:
The BufferedReader will only read until exactly 10110000 lines only (out of 25 millions lines) all the time (tried 10+ times).
Extra Info:
The google ReadChannel is still endOfStream=false at that point. But the BufferedReader's InputStreamReader has endOfFile=true.
I am able to read the full rows (25 millions) from the exact same ReadChannel by using the following:
InputStream inputStream = Channels.newInputStream(readChannel);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(500 * 1024 * 1024);
IOUtils.copy(inputStream, outputStream);
byte[] gzipFileBytes = outputStream.toByteArray();
BufferedReader reader =
new BufferedReader(
new InputStreamReader(
new GZIPInputStream(
new ByteArrayInputStream(gzipFileBytes)),
UTF_8));
Really appreciate your helps.
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"));
I'm reading the content from a file located on a server with the FTPClient from Apache Commons Net. It works fine when only reading once. But when I'm trying to read a second file, the InputStream of my FTPClient returns null. This is my code:
FTPClient ftpClient = new FTPClient();
ftpClient.connect("myhostname");
ftpClient.login("myusername", "mypassword");
// read InputStream from file
InputStream inputStream = ftpClient.retrieveFileStream("/my/firstfile.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
// read every line...
// close everything
inputStream.close();
bufferedReader.close();
// second try
inputStream = ftpClient.retrieveFileStream("/my/secondfile.txt");
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
// ...
inputStream.close();
bufferedReader.close();
What am I doing wrong?
After closing the InputStream, do the following:
ftpClient.completePendingCommand();
You can find more information in the javadoc of FTPClient#retrieveFileStream:
To finalize the file transfer you must call completePendingCommand and check its return value to verify success. If this is not done, subsequent commands may behave unexpectedly.
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/.
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);