1

我之前提出了同样的问题,但我尝试了所有给定的答案,但没有人选择我的 prog。我创建了一个写入文件的方法,并将通过其他方法使用该文件。我的问题是当我想写另一个时间。我想从文件的开头开始,所以删除内容。这是我使用的课程,if exist delete否则BufferedWriter bw = new BufferedWriter(new FileWriter(f, false));我将只有执行的最后一行。我想如果我不能在某个时间使用某些文件进行读写。

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");
      }

    }
4

2 回答 2

1

来自 javadoc 的 RandomAccessFile

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. 
于 2013-03-07T11:43:18.053 回答
1

我只是使用 printWriter 就是这样。

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();
            }
        }

      }
于 2013-03-07T11:43:24.113 回答