android bufferedwriter error - java

I'm working on an android app where I need to create a file and write/read on it using bufferedwrite and bufferedread. The file is declared in the activity as follows:
String string = "string";
File file = new File(this.getFilesDir(), string);
When I try to write to the file using this code, however:
BufferedWriter out = new BufferedWriter(new FileWriter(new File(file)));
out.write("I am a line of text written in" + file);
out.close();
I keep getting a "constructer File(File) is undefined" error. I believe the error has to do with the file declaration, but I'm not sure why.
Any help is greatly appreciated.

String string = "string";
File file = new File(this.getFilesDir(), string);
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write("I am a line of text written in" + file);
out.close();

Related

Create a file using variables for the file name java

I'm trying to create a file and save the file as a name given as input and the iteration number. But this isn't writing anything to the source folder. I don't know where I could be going wrong.
private void write() throws IOException {
File file = new File(name + it_number + ".txt");
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write(results);
it_number++;
writer.flush();
writer.close();
}
Not a fix, but a debug hint - to find where the file is going to be written:
File file = new File(name + it_number + ".txt");
System.out.println(file.getAbsolutePath());
Try to use BufferedWriter as below
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(results);
bw.close();

How to write in arabic in arraylist

I have an application that reads a file then a user updates the file with different language and saves it. But whenever anyone changes the language and saves the txt file change the letters into symbols ??? I think i have to save it as utf-8 formatted. Any idea?
The next part of code where i get the data from a jtable and save it to a file when user click at save button.
Note: there is nothing wrong with the code. The problem is that the file has to be saved as utf-8.
if(buttonPressed.equals(save)){
File myFile = new File("youFile.srt");
try (PrintWriter writer = new PrintWriter(myFile)) {
for (int i =0 ; i< cModel.getRowCount(); i++)
{
sData.add(cModel.getValueAt(i, 1).toString());
eData.add(cModel.getValueAt(i, 2).toString());
tData.add(cModel.getValueAt(i, 3).toString());
writer.println(i+1 + "\n");
writer.println(sData.get(i)+" --> " + eData.get(i));
writer.println(tData.get(i));
writer.println("\n");
}
}catch(FileNotFoundException ioEx){
ioEx.printStackTrace();
}
PrintWriter need to be clubbed with OutputStreamWriter because it offers constructors to provide the file format. Here is what you can do:
FileOutputStream outputStream = new FileOutputStream(myFile);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(
outputStream, StandardCharsets.UTF_8), true)
You are using a FileWriter. It is better to create a FileOutputStream and then use OutputStreamWriter to write the output. OutputStreamWriter allows you to specify the encoding.
Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("youFile.srt"), "UTF-8"));
try {
writer.write(aString);
} finally {
writer.close();
}

Editing file in External Storage

In my app, I am writing a file and storing it in external storage
But everytime I want to edit it, I have to get the data of file, delete it and then recreate it using the new data.
But is there any way to directly edit a existing file instead of deleteing and recreating it?
Thanks to blackbelt for helping me out.
Here is how to do this -
File gpxfile = new File(File address, "filename.txt");
BufferedWriter bW;
try {
bW = new BufferedWriter(new FileWriter(gpxfile));
bW.write("file text");
bW.newLine();
bW.flush();
bW.close();
} catch (IOException e) {
e.printStackTrace();
}
This will rewrite the file. If you just want to add a line instead of replacing the whole thing, then replace
bW = new BufferedWriter(new FileWriter(gpxfile));
with
bW = new BufferedWriter(new FileWriter(gpxfile, true));

Java save temporary data to file

I have this code:
for (Record record : Adatok) {
//System.out.println(record.toString2());
act_data=tmp.testtestclass(record);
System.out.println("*******");
System.out.println("Feldolgozás eredménye:");
System.out.println(data_restructure(act_data));
// String content = record.nev + ";" + record.address + "\n"+"asd";
File file = new File("resultset.csv");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(data_restructure(act_data) + "\n");
bw.close();
}
My problem is that this loop running time is hours and can be interupting. So I do this filewrite/bufferedwrite in it.
So everytime he get data back than I want to write it to file.
But when I do this it is always write only 1 line to my file than nothing.
How can I improve it? I tried with firewriter, bufferedwriter but It kinda bugging.
I know its a dumb question but I cant figurit out how to solve it cause the basic examples does not works.
You can try to use FileWriter with TRUE parameter:
FileWriter fw = new FileWriter(file.getAbsoluteFile(),true); //see here!
BufferedWriter bw = new BufferedWriter(fw);
By doing this, new Text will be appended to the file.
Put these lines outside (before) the loop. You are overwriting the file in each loop iteration.
File file = new File("resultset.csv");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
Also,
bw.close(); // outside (after) the loop

Writing to file in Java

First things first, I am a novice when it comes to Java. I have set myself a little project and I am currently stuck. I am trying to write to a file that already exists, but it just overwrites it. I am trying to replace the line that contains 'maxBooks'.
Here is the code than I am using:
FileWriter writeFile = new FileWriter(fileLocation);
BufferedReader readLines = new BufferedReader(new FileReader(fileLocation));
BufferedWriter writeLines = new BufferedWriter(writeFile);
System.out.println("\n-----File Begin-----");
while((finalLines = readLines.readLine()) != null){
if(finalLines.contains("maxBooks")){
writeLines.newLine();
writeLines.write(finalLines);
System.out.println("This is the if statement");
System.out.println(finalLines);
} else {
fileLines.add(new String(finalLines));
System.out.println("This is the else statement");
System.out.println(finalLines);
}
}
System.out.println("------File End------");
Please bear in mind that I have left out the try and catch. Please let me know how I can edit the text file. Let me know if you need any more info
Thanks :)
EDIT
Sorry, I should clarify. I am just trying to edit 1 line that is in the test file, not the whole text file.
FINAL CODE:
FileWriter writeFile = new FileWriter(fileLocation + ".tmp", true);
BufferedWriter writeLines = new BufferedWriter(writeFile);
BufferedReader readLines = new BufferedReader(new FileReader(fileLocation));
System.out.println("\n-----File Begin-----");
while((finalLines = readLines.readLine()) != null){
if(finalLines.contains("maxBooks")){
writeLines.write("maxBooks = " + maxBooks);
writeLines.newLine();
System.out.println("This is the if statement");
System.out.println(finalLines);
} else {
fileLines.add(new String(finalLines));
System.out.println("This is the else statement");
writeLines.write(finalLines);
writeLines.newLine();
}
}
System.out.println("------File End------");
file2.renameTo(file);
writeLines.close();
You overwrite the file you try to read, which is bad practice. Write to a new file, then rename to the original file.
you read and write on the same fileLocation you're supposed to give two differend locations;
goes something like this
//define newLocation as string that contain path for new file to be written
FileWriter writeFile = new FileWriter(newLocation);
BufferedReader readLines = new BufferedReader(new FileReader(fileLocation));
BufferedWriter writeLines = new BufferedWriter(writeFile);

Categories