Search code examples
javajxl

How do I convert from .txt to .xls file read/write?


So in my project I used to read/write data from/to .txt files, but I realized that It will be better if I would do that from an excel file. This is how I did it.

        for (File benchmarkLoop : listOfFiles) {
        String line = null;
        BufferedReader in = null;
        try {
                in = new BufferedReader(new FileReader("benchmarks\\" + benchmarkLoop.getName()));  
            } catch (FileNotFoundException fnfe) {
                fnfe.printStackTrace();
            }
            Writer writer = null;
            File file = new File("results", benchmarkLoop.getName());
            writer = new BufferedWriter(new FileWriter(file));}

Now I have to change this and I'm not so familiar with jxl.

        while (initializingIterations > 0) {
                line = in.readLine();
                writer.write(0 + System.getProperty("line.separator"));          
                markov.update(  new Integer((int) (Math.round(Float.parseFloat(line)/interval))));   
                initializingIterations--;     
            }

        while ((line = in.readLine()) != null ) 

Solution

  • Using Java 8 without any additional libraries:

    private static <T> List<List<T>> partitionList(List<T> list, int size) {
        return IntStream.range(0, (list.size() + (size - 1)) / size)
                .mapToObj(x -> list.subList(x * size, Math.min((x + 1) * size, list.size())))
                .collect(Collectors.toList());
    }