I want to read a text file and I can read with below written code. But my text file include Turkish characters like "ü", "ç", "ğ", "ö"... When I read that text file, I can see these characters. For example, my word which written in text file is "okçu" but I see on my phone like "ok?u". How can fix it?
public static String readTextFile(Context ctx, int resId) {
InputStream inputStream = ctx.getResources().openRawResource(resId);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(inputreader);
String line;
StringBuilder stringBuilder = new StringBuilder();
try {
while ((line = bufferedreader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append('\n');
}
} catch (IOException e) {
return null;
}
return stringBuilder.toString();
}
I faced this problem when I received the response from my HttpClient for the first time. I managed to solve it by specifying the encoding within the InputStreamReader object's instantiation. I hope it might help you as well.
InputStream inputStream = ctx.getResources().openRawResource(resId);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF8"),8);
Related
Hi I'm currently having issues with reading in a file in android studio. The file I want to read in is just a simple text file called test. This is the path of the file C:\Users\John\Documents\MadLibs\app\src\main\res\raw\test.txt. Here's what I'm trying:
BufferedReader br = new BufferedReader(new FileReader(getResources().openRawResourceFd(R.raw.test)));
I'm relitively new to android studio and really don't understand how to read in files. I assumed its just like java however everything I've tried fails. Anyone have any ideas?
Reading a textfile in android studio
FileInputStream fileInputStream=openFileInput("file.txt");
InputStreamReader InputRead= new InputStreamReader(fileInputStream);
char[] inputBuffer= new char[READ_BLOCK_SIZE];
String s="";
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
String rs=String.copyValueOf(inputBuffer,0,charRead);
s +=rs;
}
InputRead.close();
Log.d(TAG,s);
To read a text file as a string, you can use the following method:
// #RawRes will gives you warning if rawId is not from correct id for raw file.
private String readRawFile(#RawRes int rawId) {
String line;
try {
InputStream is = getResources().openRawResource(rawId);
// read the file as UTF-8 text.
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// Or using the following if API >= 19
//BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
StringBuilder result = new StringBuilder();
while ((line = reader.readLine()) != null) {
result.append(line);
}
reader.close();
inputStream.close();
line = result.toString();
} catch (IOException e) {
line = null;
}
return line;
}
I want to download a file using FTP, and then keep the file in memory and simply loop over the lines.
I am downloading the file like this so far:
ByteArrayOutputStream bos = new ByteArrayOutputStream()
ftp.retrieveFile("inventory.csv", bos)
I'm not sure how to go from a ByteArrayOutputStream to be able to loop each line.
https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#retrieveFile(java.lang.String,%20java.io.OutputStream)
This is the idea I had in mind, save minor overlooks, it should help you:
ByteArrayOutputStream bos;
BufferedReader bufferedReader;
FTPClient ftp;
String line;
ArrayList<String[]> values;
try{
bos = new ByteArrayOutputStream();
ftp = new FTPClient();
if (ftp.retrieveFile("inventory.csv", bos)){
values = new ArrayList<>();
bufferedReader = new BufferedReader(new StringReader(new String(bos.toByteArray())));
while ((line = bufferedReader.readLine()) != null){
values.add(line.split(","));
}
// Then you could switch ArrayList<String[]> for just String[]
}
}
catch(Exception x){
}
finally{
if (bufferedReader != null)
bufferedReader.close();
if (bos != null)
bos.close();
}
You could use retrieveFileStream() instead and then read line by line in a try-with-resources block using a BufferedReader.
try (BufferedReader reader =
new BufferedReader(
new InputStreamReader(ftp.retrieveFileStream("inventory.csv"),
StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
// do something with line
}
} catch (IOException e) {
// handle error
}
The code assumes that the file is encoded in UTF-8. If a different encoding is used make sure to replace StandardCharsets.UTF_8 accordingly.
The advantage over an intermediate ByteArrayOutputStream is that at no point your program needs to have a copy of the whole file content in memory.
I am trying to copy some data from pdf to txt file here is the code
public void readPDFFile() throws IOException {
InputStreamReader reader;
OutputStreamWriter writer;
FileInputStream inputstream;
FileOutputStream outputStream;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
String str;
File rfile = new File(
"C://Documents and Settings/Administrator/My Documents/EGDownloads/source.pdf");
File wFile = new File("C://Documents and Settings/Administrator/My Documents/Folder/destination.txt");
try {
inputstream = new FileInputStream(rfile);
outputStream = new FileOutputStream(wFile);
reader = new InputStreamReader(inputstream, "UTF-8");
writer = new OutputStreamWriter(outputStream, "UTF-8");
bufferedReader = new BufferedReader(reader);
bufferedWriter = new BufferedWriter(writer);
while ((str = bufferedReader.readLine()) != null) {
writer.write(str);
}
} catch (IOException es) {
System.out.println(es.getMessage());
es.printStackTrace(System.out);
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
if (bufferedWriter != null)
bufferedWriter.close();
}
}
Expected output is supposed in other language but all I am getting is some random boxes as tried both UTF-16 and UTF-8 unicodes
I tried pdfBox but is still not working as all I'm getting is only original language accent and in english language
Note :
1 I'm not trying to print data on console but copying from pdf to txt file
2 Other file contains non english words,
can anyone help me to solve that??
Or any link that might help
Thanks.
The PDF format is a binary format. You must have a really special PDF as all that I know of are compressed in some way. Use a proper library to read it, be it pdfbox or itext or other. Be aware that in some PDFs it's impossible to extract text, you can check it with Acrobat, if Acrobat can't do it nobody can.
I am trying to read a file using BufferedReader, but when I tried to print, It is returning some weird characters.
Code of reading file is:
private static String readJsonFile(String fileName) throws IOException{
BufferedReader br = null;
try {
StringBuilder sb = new StringBuilder();
br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
while(line != null ){
sb.append(line);
System.out.println(line);
line=br.readLine();
}
return sb.toString();
} finally{
br.close();
}
}
This function is being called as :
String jsonString = null;
try {
jsonString = readJsonFile(fileName);
} catch (IOException e) {
e.printStackTrace();
}
But when I tried to print this in console using System.out.println(jsonString);, It is returning some fancy pictures.
Note: It is Working file when file size is small.
Is there any limit on size of file it can read ?
You're using the platform default encoding to read the file, which is probably encoded in UTF8. Check the actual encoding of the file, and specify the encoding:
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream("...", StandardCharsets.UTF_8));
Note that since you simply want to read everything from the file, you could simply use
String json = new String(Files.readAllBytes(...), StandardCharsets.UTF_8);
I've a XML file and want to send its content to caller as string. This is what I'm using:
return FileUtils.readFileToString(xmlFile);
but this (or that matter all other ways I tried like reading line by line) escapes XML elements and enclose whole XML with <string> like this
<string>><.....</string>
but I want to return
<a>....</a>
I'd advise using a different file reader maybe something like this.
private String readFile( String file ) throws IOException {
BufferedReader reader = new BufferedReader( new FileReader (file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
while( ( line = reader.readLine() ) != null ) {
stringBuilder.append( line );
stringBuilder.append( ls );
}
return stringBuilder.toString();
}
It's probably a feature of file utils.
According to your question you just want to read the file. You can use FileReader and BufferedReader to read the file.
File f=new File("demo.xml");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
String line;
while((line=br.readLine())!=null)
{
System.out.println(line);
}
Hope this answer helps you
IOUtils works well. It's in package org.apache.commons.io. The toString method takes an InputStream as a parameter and returns the contents as a string maintaining format.
InputStream is = getClass.getResourceAsStream("foo.xml");
String str = IOUtils.toString(is);
BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
String line;
StringBuilder sb = new StringBuilder();
while((line = br.readLine())!= null){
sb.append(line.trim());
}