1

I posed the same question earlier but i tried all the given answer and no one go with my prog. I created a method that write in a file and the will use this file by an other method. my problem is when i would like to write an other time . i would like to start from the begining of the file so erase the content. this my class when i use if exist delete or BufferedWriter bw = new BufferedWriter(new FileWriter(f, false)); i will have just the last line of the execution. and i would like if i cant use the some file for read and write in the some time.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.swing.SwingWorker;

/**
 * 
 * @author 
 */
 class InstallApk extends SwingWorker<Integer, Integer>{

    // File wd = new File("/myhomedir/");
    private InputStream pi;
    public static String dir;
    public static String cmd;
    public static String log;
    public static String testPath ;
    public static ArrayList<String> mlog = new ArrayList();
    public static String userHome = System.getProperty("user.home");
    public static int value = 0;
    private static Process runningProcess = null;
    public static Process process;
    static File f = new File(testPath + "/logtest.txt");
    private static final int DELAY = 1000;


    public InstallApk() {

    }

    public static String getPath() {
        String decodedPath = null;
        try {
            String path = NewJFrame.class.getProtectionDomain()
                    .getCodeSource().getLocation().getPath();
                    String f = NewJFrame.class.getProtectionDomain().getCodeSource().getLocation().getFile();
                    //.getCodeSource().getLocation().getPath();
            // URLDecoder decoder = new URLDecoder();
            decodedPath = URLDecoder.decode(path, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();

        }
        return decodedPath;
    }


    public static void writeToFile(String text) {
        try {

            BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
            //FileWriter out = new FileWriter(f);
            bw.write(text);
            bw.newLine();
            bw.close();
            //out.close();

        } catch (Exception e) {

        }
    }


      protected Integer doInBackground() throws Exception {
        int i = 0;
        int count = 10;
        Runtime runtime = Runtime.getRuntime();
        process = runtime.exec(new String[] { "sh",
                testPath + "/install.sh", cmd });
        while (!isCancelled() && i < count) {
          i++;
          publish(new Integer[] { i });
          setProgress(count * i / count);
          Thread.sleep(DELAY);
        }

     // read process output first
        BufferedReader buf = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        String line = "";
        try {

            if(f.exists()){
                 f.delete();

             }
            while ((line = buf.readLine()) != null) {
                System.out.println("exec response: " + line);
                if(line.startsWith("Failure")){

                    mlog.add(line);

                }
                log = line;
                writeToFile(line);
                //WriteFile.write(line, f);
                value ++;
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
         System.out.println("la valeur d'execution "+InstallApk.value);
        // only once we've run out of output can we call waitFor
        while (true) {
            try {
                return runningProcess.waitFor();
            } catch (InterruptedException e) {
                // do nothing, wait again
            } finally {
                Thread.interrupted();
            }
        }

      }

      protected void processus(List<Integer> chunks) {
        for (int i : chunks)
          System.out.println(i);
      }

      protected void arret() {
        if (isCancelled())
          System.out.println("Annuler");
        else
          System.out.println("Terminer");
      }

    }
Community
  • 1
  • 1
user2043602
  • 237
  • 2
  • 4
  • 15

2 Answers2

1

RandomAccessFile From javadoc

Instances of this class support both reading and writing to a 
* random access file. A random access file behaves like a large 
* array of bytes stored in the file system. 
StanislavL
  • 56,971
  • 9
  • 68
  • 98
1

I just use printWriter and that is it.

protected Integer doInBackground() throws Exception {
        int i = 0;
        int count = 10;
        Runtime runtime = Runtime.getRuntime();
        process = runtime.exec(new String[] { "sh",
                testPath + "/install.sh", cmd });
        while (!isCancelled() && i < count) {
          i++;
          publish(new Integer[] { i });
          setProgress(count * i / count);
          Thread.sleep(DELAY);
        }
         File f = new File(testPath + "/logtest.txt");
     // read process output first
        BufferedReader buf = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
        String line = "";
        try {


            PrintWriter writer = new PrintWriter(f, "UTF-8");



            while ((line = buf.readLine()) != null) {
                System.out.println("exec response: " + line);
                if(line.startsWith("Failure")){

                    mlog.add(line);

                }
                log = line;
                //writeToFile(line);
                writer.println(line);
                //WriteFile.write(line, f);
                value ++;
            }
            writer.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
         System.out.println("la valeur d'execution "+InstallApk.value);
        // only once we've run out of output can we call waitFor
        while (true) {
            try {
                return runningProcess.waitFor();
            } catch (InterruptedException e) {
                // do nothing, wait again
            } finally {
                Thread.interrupted();
            }
        }

      }
user2043602
  • 237
  • 2
  • 4
  • 15