1

以前に同じ質問をしましたが、与えられた答えをすべて試しましたが、誰も私のプログラムに賛成しませんでした。ファイルに書き込むメソッドを作成し、このファイルを別のメソッドで使用します。私の問題は、別の機会に書きたいときです。ファイルの先頭から始めたいので内容を消去します。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 に答える