I've just created a file in MainActivity using the code:
FileOutputStream outputStream = openFileOutput("user", Context.MODE_PRIVATE);
outputStream.close();
Now what to use if I want to read the file (noting that the file will be obviously created in the internal storage)?
And is there a way that makes the file reading works as the Scanner function in Java (where the string is being read word by word and line by line)?
Use openFileInput() with the same parameters as openFileOutput().
This is your solution what I understood from your question,
FileInputStream fIn = new FileInputStream(new File("FILE_PATH"));
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(fIn));
String str = bufferedReader.readLine();
Related
I have a list of french words where I am trying to search in my database. The words are "thé Mariage frères", "thé Lipton" etc.
While I am reading my file in java, it shows the words as "thé Lipton", "thé Mariage frères". It fails to get the correct words.
I don't know how to correct my errors.
Help me, please!!!
You file is in one encoding (maybe latin1/iso-8859-1) and you're reading your file in another encoding.
See if this port helps How to read a file in Java with specific character encoding?
Try this.
try (FileInputStream fis = new FileInputStream("input.txt");
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(isr)) {
String line;
while ((line = reader.readLine()) != null)
System.out.println(line);
}
Try creating Scanner object like this
Scanner s = new Scanner(new File("French_Tea_keywords/filter_keywords.txt"), "UTF8");
I have a simple java program that reads a line from file and writes it to another file.
My source file has words like: it's but the destination file is having words like it�s.
I am using BufferedReader br = new BufferedReader(new FileReader(inputFile)); to read the source file and PrintWriter writer = new PrintWriter(resultFile, "UTF-8"); to write the destination file.
How to get the actual character in my destination file too?
You need to specify a CharacterSet when creating the BufferedReader, otherwise the platform default encoding is used:
BufferedReader br = new BufferedReader(new FileReader(inputFile),"UTF-8");
I know this question is a bit old, but I thought I'd put down my answer anyway.
You can use java.nio.file.Files to read the file and java.io.RandomAccessFile to write to your destination file. For example:
public void copyContentsOfFile(File source, File destination){
Path p = Paths.get(source.toURI());
try {
byte[] bytes = Files.readAllBytes(p);
RandomAccessFile raf = new RandomAccessFile(destination, "rw");
raf.writeBytes(new String(bytes));
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
The code says:
PrintWriter write = new PrintWriter("newFile.txt");
write.println("TransformersLegends");
What is the best way to read from the file? I can't use File or Buffered Reader as it takes a file object and here I'm trying with a string.
What is the best way to read from the file?
You can use BufferedReader this way :
final String PATH = "newFile.txt";
BufferedReader br = new BufferedReader(new FileReader(PATH));
Just decorate the new FileReader with a new BufferedReader.
You can use a FileReader.
It have a constructor new FileReader(String path).
As said in commentary, a BufferedReader will be more efficient, you can instantiate it with a string too new BufferedReader(new FileReader("FILE_PATH"))
How can I convert InputStreamReader to InputStream? I have an InputStream which contains some string and byte data and I want to parse it. So I wrap my InputStream to BufferedReader. Then I read 3 lines from it. After that I want to get the rest of data(bytes) as is. But if I try to get it nothing happens.
Code snippet:
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String endOfData = br.readLine();
String contentDisposition = br.readLine();
String contentType = br.readLine();
file = new File(filename);
if(file.exists()) file.delete();
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
byte[] data = new byte[8192];
int len = 0;
while (-1 != (len = is.read(data)) )
{
fos.write(data, 0, len);
Log.e("len", len+"");
}
fos.flush();
fos.close();
is.close();
The file is empty. If I don't wrap InputStream it works fine, but I need to read 3 lines and remove it.
Thanks.
If you want to mix text and byte data together, you should use OutputStream.writeUTF to write out those 3 lines, this way one single InputStream will be able to retrieve all the data that you need.
Take a look at commons-io's ReaderInputStream: it is a little heavy handed, but you can wrap the BufferedReader with that and read it as an input stream again.
It's pretty hard to mix byte and character input correctly, especially once you start throwing buffered readers / streams into the mix. I'd suggest that you either pick one and stick with it (converting your bytes to strings as necessary; care with the encoding!) or wrap the entire thing in a ZipOutputStream so you can have multiple logical "files" with different contents.
Hi am having a csv file in my hand and am trying to read each line in csv file and update the content to a database table. Am doing this using java.
Following are the things i did for achieving this.
FileInputStream fileInputStream = FileUtils.openInputStream("filename.csv");
dataInputStream = new DataInputStream(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
String strLine;
while((strLine = bufferedReader.readLine()) != null){
System.out.println(strLine);
}
but its printing something like blocks on the console, not the actual data csv having
can anyone please help me to solve this issue.?
you should find out what encoding it is and then declare it while reading, xample for UTF-16:
new InputStreamReader(zipFile.getInputStream(entry), "UTF_16" )