• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Android studio copy the contents of a .txt file to another .txt file

Hi everyone, in my application I have a file called "commands.txt", I want to make a button to copy the contents of "commands.txt" file to another ".txt" file that will be created, but I could not.Thank you very much if you help.
 
Hello again.I managed to write my code after a little struggle.I'm writing here because it might be someone who needs this code.I wish everyone a good day.
Code:
save_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        File file = new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt");

        StringBuilder text = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
            }
            br.close();
        }
        catch (IOException e) {
        }

     try{
            String str="komutlar";
            String tonun=String.valueOf(number);
            str=str+tonun;

            File filepath=new File("/data/data/com.example.emrecan.myapplication/files",str+".txt");
            FileWriter writer=new FileWriter(filepath);
            writer.append(text);
            writer.flush();
            number++;
            writer.close();

            Toast.makeText(getApplicationContext(), "Kaydetme Tamamlandı!", Toast.LENGTH_LONG).show();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }


    }
});
 
Good job on figuring it out. :thumbsupdroid: Just like to give some advice. I noticed you made a common mistake that new developers make(not a bad one). Your code should be in 3 separate methods to make your code modular and reusable. Remember java is an oop language(object oriented). So you want to make your objects reusable.

By having your read file method in its own object that accepts a variable and returns a string, you can reuse the read method anywhere in your app. Same goes for the write to file method. Then your button should just pass the variable to the method.

Here's an example of a reusable method I use to read a file which I can reuse anywhere as many times in my app. (keep in mind I wrote this for a desktop app, but should fit right in on android)

Java:
public static String getFileContent(String file) {
    String content = "";
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            line = br.readLine();
            if (line != null) {
                sb.append("\n");
            }
        }
        content = sb.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return content;
}

Now I can get contents of any file through this one object like so...

Java:
getFileContent("/path/to/filename");

Now lets say you have a separate write to file method, then passing the variable would look something like this assuming the method is called writeToFile()...

Java:
writeToFile(getFileContent("/path/to/filename"));

I hope this sample helps you or anyone else think about breaking their code into smaller reusable objects rather than writing the same code over and over.

Cheers :)
 
Last edited:
Good job on figuring it out. :thumbsupdroid: Just like to give some advice. I noticed you made a common mistake that new developers make(not a bad one). Your code should be in 3 separate methods to make your code modular and reusable. Remember java is an oop language(object oriented). So you want to make your objects reusable.

By having your read file method in its own object that accepts a variable and returns a string, you can reuse the read method anywhere in your app. Same goes for the write to file method. Then your button should just pass the variable to the method.

Here's an example of a reusable method I use to read a file which I can reuse anywhere as many times in my app. (keep in mind I wrote this for a desktop app, but should fit right in on android)

Java:
public static String getFileContent(String file) {
    String content = "";
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            line = br.readLine();
            if (line != null) {
                sb.append("\n");
            }
        }
        content = sb.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return content;
}

Now I can get contents of any file through this one object like so...

Java:
getFileContent("/path/to/filename");

Now lets say you have a separate write to file method, then passing the variable would look something like this assuming the method is called writeToFile()...

Java:
writeToFile(getFileContent("/path/to/filename"));

I hope this sample helps you or anyone else think about breaking their code into smaller reusable objects rather than writing the same code over and over.

Cheers :)

What you're saying is a lot more logical, thank you for the code :). I'll edit and use it for my program.
 
Back
Top Bottom