← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #278890]: read and write

 

Question #278890 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/278890

RaiMan proposed the following answer:
if I understood you right, this should do what you want.

package test;

import java.io.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;

public class Test {

  public static void main(String[] args)
          throws FileNotFoundException, UnsupportedEncodingException, IOException {
    BufferedReader input;
    String line;
    File intermediate = new File("myfile1.txt");
    PrintWriter output = new PrintWriter(intermediate, "UTF-8");
    String p = "New folder";
    File dir = new File(p);
    /**
     * collect all files in a directory and store their content 
     * line by line in one file (intermediate)
     * with a header line per file before the content of the file
     */
    File[] files = dir.listFiles();
    for (File f : files) {
      if (f.isFile()) {
        input = new BufferedReader(new FileReader(f));
        line = String.format("[%s_DINAMALAR_DATE]", f.getName());
        output.println(line);
        System.out.println(line);
        while ((line = input.readLine()) != null) {
          output.println(line);
          System.out.println(line);
        }
        input.close();
      }
    }
    output.close(); 
    /**
     * read the content of file intermediate and create a new file 
     * for each file block contained in the input file
     */
    input = new BufferedReader(new FileReader(intermediate));
    int count = 0;
    output = null;
    while ((line = input.readLine()) != null) {
      if (line.startsWith("[")) {
        if (output != null) output.close();
        output = new PrintWriter(new File(line + ".txt"), "UTF-8");
        output.println(line);
        count++;
        continue;
      }
      output.println(line);
    }
    input.close();
  }
}

-- 
You received this question notification because your team Sikuli Drivers
is an answer contact for Sikuli.