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

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/.

Related

Open a BufferedReader in UTF-8

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"));

How to close FTPClient FileStream properly

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.

Read any file as a binary string

As the title suggests, is there any way to read a binary representation of a given file (.txt, .docx, .exe, etc...) in Java (or any other language)?
In java, I know how to read the content of a file as is, i.e:
String line;
BufferedReader br = new BufferedReader(new FileReader("myFile.txt"));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
But I'm not sure (if it's possible) to read a binary representation of the file itself.
File file = new File(filePath);
byte[] bytes = new byte[(int)file.length()];
DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
dataInputStream.readFully(bytes);
dataInputStream.close();
bytes is a byte array with all of the data of the file in it

How to write in directory of program?

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.

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