1. Are you ready for the Galaxy S20? Here is everything we know so far!

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

Discussion in 'Android Development' started by Emre Can AYDIN, May 14, 2019.

  1. Emre Can AYDIN

    Thread Starter

    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.
     


  2. Emre Can AYDIN

    Thread Starter

    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 (Text):
    1.  
    2. save_btn.setOnClickListener(new View.OnClickListener() {
    3.     @Override
    4.     public void onClick(View v) {
    5.  
    6.         File file = new File("/data/data/com.example.emrecan.myapplication/files/komutlar.txt");
    7.  
    8.         StringBuilder text = new StringBuilder();
    9.         try {
    10.             BufferedReader br = new BufferedReader(new FileReader(file));
    11.             String line;
    12.  
    13.             while ((line = br.readLine()) != null) {
    14.                 text.append(line);
    15.                 text.append('\n');
    16.             }
    17.             br.close();
    18.         }
    19.         catch (IOException e) {
    20.         }
    21.  
    22.      try{
    23.             String str="komutlar";
    24.             String tonun=String.valueOf(number);
    25.             str=str+tonun;
    26.  
    27.             File filepath=new File("/data/data/com.example.emrecan.myapplication/files",str+".txt");
    28.             FileWriter writer=new FileWriter(filepath);
    29.             writer.append(text);
    30.             writer.flush();
    31.             number++;
    32.             writer.close();
    33.  
    34.             Toast.makeText(getApplicationContext(), "Kaydetme Tamamlandı!", Toast.LENGTH_LONG).show();
    35.         }
    36.         catch (FileNotFoundException e)
    37.         {
    38.             e.printStackTrace();
    39.         }
    40.         catch (IOException e)
    41.         {
    42.             e.printStackTrace();
    43.         }
    44.  
    45.  
    46.     }
    47. });
    48.  
     
    23tony likes this.
  3. GameTheory

    GameTheory Android Expert

    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)

    Code (Java):
    1. public static String getFileContent(String file) {
    2.     String content = "";
    3.     try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    4.         StringBuilder sb = new StringBuilder();
    5.         String line = br.readLine();
    6.         while (line != null) {
    7.             sb.append(line);
    8.             line = br.readLine();
    9.             if (line != null) {
    10.                 sb.append("\n");
    11.             }
    12.         }
    13.         content = sb.toString();
    14.     } catch (IOException e) {
    15.         e.printStackTrace();
    16.     }
    17.     return content;
    18. }
    Now I can get contents of any file through this one object like so...

    Code (Java):
    1. 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()...

    Code (Java):
    1. 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 :)
     
    #3 GameTheory, May 14, 2019
    Last edited: May 14, 2019
    Emre Can AYDIN and 23tony like this.
  4. Emre Can AYDIN

    Thread Starter

    What you're saying is a lot more logical, thank you for the code :). I'll edit and use it for my program.
     
    GameTheory likes this.
Loading...
Similar Threads - Android studio copy
  1. Syeda Quratulain
    Replies:
    1
    Views:
    288
  2. Sheraz Ahmed
    Replies:
    0
    Views:
    178
  3. Nieve1
    Replies:
    0
    Views:
    315
  4. docco2020
    Replies:
    1
    Views:
    471
  5. lovingdog
    Replies:
    0
    Views:
    637
  6. Gleis
    Replies:
    4
    Views:
    828
  7. emamsa
    Replies:
    0
    Views:
    454
  8. saran ks
    Replies:
    0
    Views:
    373
  9. mdsousa
    Replies:
    0
    Views:
    494
  10. Ajaykumar Panchamoorthy
    Replies:
    2
    Views:
    226

Share This Page

Loading...