This question already has answers here:
android reading from a text file
(3 answers)
How to load text from file to textview [duplicate]
(3 answers)
Closed 9 years ago.
My problem with the software I'm trying to make is this;
when I save the information, it's saved with this code:
With this, the information is saved in a text in the SD,
but I couldn't to read that information. I've tried with several ways.
I want to save the content of the TXT in a String to put it in a TextView.
try
{
String myData ;
FileInputStream fis = new FileInputStream(your file name);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// play with myData..It's file content
I have tried this code and it works perfectly :D
File ruta_sd = Environment.getExternalStorageDirectory();
File f = new File(ruta_sd.getAbsolutePath(), "datos.txt");
BufferedReader fin =new BufferedReader(
new InputStreamReader(
new FileInputStream(f)));
String texto = fin.readLine();
fin.close();
textohere.setText(texto);
Related
This question already has answers here:
Find a line in a file and remove it
(17 answers)
Closed 5 years ago.
I need to remove lines from txt file a
FileReader fr= new FileReader("Name3.txt");
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
br.close();
and I don't know the continue of the code.
You can read all lines and store them in a list. Whilst you're storing all lines, assuming you know the line you want to remove, simply check for the lines you don't want to store, and skip them. Then write the list content to a file.
//This is the file you are reading from/writing to
File file = new File("file.txt");
//Creates a reader for the file
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
//This is your buffer, where you are writing all your lines to
List<String> fileContents = new ArrayList<String>();
//loop through each line
while ((line = br.readLine()) != null) {
//if the line we're on contains the text we don't want to add, skip it
if (line.contains("TEXT_TO_IGNORE")) {
//skip
continue;
}
//if we get here, we assume that we want the text, so add it
fileContents.add(line);
}
//close our reader so we can re-use the file
br.close();
//create a writer
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
//loop through our buffer
for (String s : fileContents) {
//write the line to our file
bw.write(s);
bw.newLine();
}
//close the writer
bw.close();
This question already has answers here:
How to read a specific line using the specific line number from a file in Java?
(19 answers)
Closed 5 years ago.
My program is reading all lines in the file but i just need the second one.
String line;
try (
InputStream fis = new FileInputStream(source);
InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(isr)) {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
If you only need the second line and you are sure the file always has at least two lines, you can just read twice and ignore the first time.
br.readLine(); //read, but ignore
System.out.println(br.readLine()); // read and output
This question already has answers here:
What is simplest way to read a file into String? [duplicate]
(9 answers)
Java 1.4.2 - Reading Files
(3 answers)
Closed 7 years ago.
I need to read text file to String. I do :
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filePath));
String line = br.readLine();
String everything = line;
while (line != null) {
line = br.readLine();
everything += line + "\n";
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
But I dont like to read line by line. It should be possible to read whole file to string at one function call. I'm right? By the way, I must use java 1.4.
you can read the whole file data as byte array.
File file = new File(yourFileName);
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
String strData=new String(data, "UTF-8"); // converting byte array to string
System.out.println(strData);
If you are allowed to use an external library, you could have a look on Apache Commons FileUtils
Besides, you definitely should not use such an old java version.
I've tried searching for questions like mine, found alot but non of the answers worked for me.
I'm working with Android studio and trying to open a text file from a java class, but no matter what I do or how I'm trying to open it - I'm getting this error:
"... open failed: ENOENT (No such file or directory)"
As you can see - I also tried two options:
1. creating a "File" class (then at the watches window I tried to invoke - "canRead()" function but getting back "false" value.
2. trying to send the ctor of "FileReader" class the path of my file.
non of them worked.
thanks alot!
public void fillDatabase(){
File file = new File("C:\\fillStops.txt");
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + STOPS_TABLE_NAME);
try {
FileInputStream fileInputStream = new FileInputStream(file);
FileReader fileReader = new FileReader("C:\\fillStops.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
this.addStop(line);
}
fileReader.close();
}
catch (Exception e){
String s = e.getMessage();
}
}
Try this, and place your textfile at the root of your sdcard else change the path for file in the following code.
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
String aDataRow = "";
while ((aDataRow = br.readLine()) != null) {
line+= aDataRow + "\n";
}
Log.d(">>>>>", line);
}
catch (IOException e) {
//You'll need to add proper error handling here
}
Hop it will clear the things for you. :)
P.S
Don't forget to add the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> permission into your manifest.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create a Java String from the contents of a file
I have a .txt file that I want to save in a String variable. I imported the file with File f = new File("test.txt");. Now I am trying to put the contents of it in a String variable. I can not find a clear explanation for how to do this.
Use a Scanner:
Scanner file = new Scanner(new File("test.txt"));
String contents = file.nextLine();
file.close();
Of course, if your file has multiple lines you can call nextLine multiple times.
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
String everything = sb.toString();
} finally {
br.close();
}