Cassie Cassie - 5 months ago 15
Android Question

How to insert a string to next line in android?

I am saving webView history in a .txt file like this-

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// save to history.txt
HistoryHelper file = new HistoryHelper(view.getContext());
file.writeToSD(url.toStringg());
}


this is working fine, however, it saves only one line and that's pretty obvious because of
url.toString
. how do i make it insert lines instead of replacing? i have already tried -

HistoryHelper file = new HistoryHelper(view.getContext());
file.writeToSD(url.substring('\n'));


HistoryHelper

public class HistoryHelper {

String TAG = "MyFile";
Context context;

public HistoryHelper(Context context) {
this.context = context;
}

public Boolean writeToSD(String text) {
Boolean write_successful = false;
File root = null;
try {
// check for SDcard
root = Environment.getExternalStorageDirectory();
Log.i(TAG, "path.." + root.getAbsolutePath());

// check sdcard permission
if (root.canWrite()) {
File fileDir = new File(
Environment.getExternalStorageDirectory() + "/AVD/");
fileDir.mkdirs();

File file = new File(fileDir, "History.txt");
FileWriter filewriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(filewriter);
out.write(text);
out.close();
write_successful = true;
Toast.makeText(context, "success!", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
Log.e("ERROR:---",
"Could not write file to SDCard" + e.getMessage());
write_successful = false;
Toast.makeText(context, "operation failed!", Toast.LENGTH_LONG)
.show();
}
return write_successful;
}
}

Answer

sir update your file write or create method by this code

 public Boolean writeToSD(String text) {
        Boolean write_successful = false;
        File root = null;
        try {
            // check for SDcard
            root = Environment.getExternalStorageDirectory();
            Log.i(TAG, "path.." + root.getAbsolutePath());

            // check sdcard permission
            if (root.canWrite()) {
                File fileDir = new File(
                        Environment.getExternalStorageDirectory() + "/AVD/");
                fileDir.mkdirs();

                File file = new File(fileDir, "History.txt");
                if (file.exists()) {
                    try {
                       FileWriter filewriter = new FileWriter(file, true);
                       BufferedWriter out = new BufferedWriter(filewriter);
                       out.write("\n" + text);
                       out.close();
                    } catch (Exception e) {

                    }
                } else {
                    file.createNewFile();
                    FileWriter filewriter = new FileWriter(file);
                    BufferedWriter out = new BufferedWriter(filewriter);
                    out.write(text);
                    out.close();
                }
                write_successful = true;
                Toast.makeText(context, "success!", Toast.LENGTH_LONG).show();
            }
        } catch (IOException e) {
            Log.e("ERROR:---",
                    "Could not write file to SDCard" + e.getMessage());
            write_successful = false;
            Toast.makeText(context, "operation failed!", Toast.LENGTH_LONG)
                    .show();
        }
        return write_successful;
    }
Comments