I am building an android app where one of the functionality is that i write on a .txt file and late try to read it. Its very simple. But when I try to read the file using a BufferedReader it only gives me the name of the file and not the content withing.Below is my code(just the concerned portion)
File curFile = new File(getFilesDir().getAbsolutePath()+"/Notes/"+fileName);
BufferedReader br = new BufferedReader(new FileReader(curFile));
StringBuilder note = new StringBuilder(500);
String content;
while((content = br.readLine())!= null) {
ShowToast("Inside While: "+ content);//prints only fileName
note.append(content);
note.append('\n');
}
what is it that i am doing wrong?!
Related
I'm creating a game and level editor where levels are stored in .txt files. On opening, the app opens a dialog to choose a file. This works perfectly except it can't read files from outside the classpath. What is a simple way to make it able to read a file from outside the classpath?
For getting the file I use:
public void getLevelPath() {
FileDialog dialog = new FileDialog((Frame) null, "Select File to Open");
dialog.setDirectory("C://");
dialog.setMode(FileDialog.LOAD);
dialog.setVisible(true);
String file = dialog.getFile();
System.out.println(file + " chosen.");
levelPath = file;
}
And for reading I use:
InputStream is = this.getClass().getResourceAsStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
Thank you for your help!
File inputFile = new File(path);
InputStream is = new FileInputStream(inputFile );
BufferedReader br = new BufferedReader(new InputStreamReader(is));
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'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());
}
File employe = new File("E:five/emplo.xml");
File stud = new File("E:/one/two/student.xml");
how to combine these two files in one file object
If you want to merge two standard text files then you can just use filewriters and filereaders.
I assuming that this is not some xml specific thing as I am not experienced with them.
Here is how to read a file (without the exception handling):
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr); // the only reason I use this is because I am used to line by line handling
String line;
while((line = br.readLine()) != null)
{
// do something with each line
}
You could read each file into an arraylist of strings then output using:
FileWriter fout = new FileWriter(file, toAppend);
fout.write(msg);
fout.close();
String[] filenames = new String[]{ "emplo.xml", "student.xml"};
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream("merged.xml");
for (String filename : filenames) {
InputStream inputStream = new BufferedInputStream(new FileInputStream(filename);
org.apache.commons.io.IOUtils.copy(inputStream, outputStream);
inputStream.close();
}
outputStream.close();<br/>
or you can also use SAXParser
I have a file of words in text.I want to read the file
FileInputStream fstream = new FileInputStream(s);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
MaxentTagger tagger = new MaxentTagger("tag/wsj-0-18-bidirectional-distsim.tagger");
String tagged = tagger.tagString(br);
My problem is it should read the file and give line by line of the file as string to the tagger and print in a output file.
As both input and output are going to be text, I'd use Reader and Writer rather than streams. Something like:
try (
BufferedReader in = new BufferedReader(new FileReader("inputFile.txt"));
PrintWriter out = new PrinterWriter(new FileWriter("outputFile.txt"));
) {
MaxentTagger tagger = new MaxentTagger("tag/wsj-0-18-bidirectional-distsim.tagger");
String line;
while ((line = in.readLine()) != null) {
String tagged = tagger.tagString(line);
out.println(tagged);
}
}
Note, this code uses Java 7 resource handling, so the in and out are closed automatically.