Solved

Splitting a file into batches

Posted on 2012-03-15
6
266 Views
Last Modified: 2012-03-16
Hi:
I have a text file, extracted from a database. It is a set of instructions for a saw.
Note that column eight, beginning 169... changes from 169022 to 169023, etc.
Is there any way of breaking the file at each change of batch; creating a new file, and each file having the name of the  batch it contains. So currently the file is called 169022. I need that to be JUST 169022, with different files for each subsequent batch.
I can from the database create a text file with a list of the batches if needs be.
Java is good - but will accept anything (c++, c#, vb, etc!)

Thanks!
0
Comment
Question by:ClaytonGlass
  • 3
  • 2
6 Comments
 

Author Comment

by:ClaytonGlass
ID: 37724384
Sorry...forgot the file...
0
 

Author Comment

by:ClaytonGlass
ID: 37724396
Sorry...forgot the file...
169022.txt
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37725647
You might try this class, which contains a rather crude implementation of T for your file (but it seems to work)
import java.io.*;

import java.util.*;


public class ComparableSublistRetriever<T extends Comparable<T>>
    implements Iterator<List<T>> {
    private List<T> delegate;
    private T previous;
    private T current;
    private int index;

    public ComparableSublistRetriever(List<T> items) {
        delegate = new ArrayList<T>(items);
        Collections.sort(delegate);
    }

    @Override
    public boolean hasNext() {
        return index < delegate.size();
    }

    @Override
    public List<T> next() {
        List<T> sublist = new ArrayList<T>();
        T current = delegate.get(index++);
        sublist.add(current);
	previous = current;
	while(index < delegate.size() && (current = delegate.get(index)).equals(previous)) {
            sublist.add(current);
            previous = current;
	    index++;

        }
        return sublist;
    }

    @Override
    public void remove() {
        ;
    }

    static class CsvLine implements Comparable<CsvLine> {
	private String[] array;

	public CsvLine(String[] array) {
	    this.array = array;
	}

	public int compareTo(CsvLine other) {
	    return array[7].compareTo(other.array[7]);
	}

	public boolean equals(Object other) {
	    return array[7].equals(((CsvLine)other).array[7]);
	}

	public String toString() {
	    return Arrays.toString(array);
	}
    }

    public static void main(String[] args) throws Exception {
	List<CsvLine> csvLines = new ArrayList<CsvLine>();
	Scanner s = new Scanner(new File(args[0]));
	while(s.hasNextLine()) {
	    csvLines.add(new CsvLine(s.nextLine().split("\\s+", 9)));
	}
	s.close();
        ComparableSublistRetriever<CsvLine> r = new ComparableSublistRetriever<CsvLine>(csvLines);

        while (r.hasNext()) {
            System.out.println(r.next());
        }
    }
}

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 47

Expert Comment

by:for_yan
ID: 37726247
This is the code for the complete program which does it;
See attached the files produeced from your input file
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;

public class SplitFile {
    public SplitFile() {
        try{
            HashMap <String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
            ArrayList<String> names = new ArrayList<String>();

            BufferedReader br = new BufferedReader((new FileReader("169022.txt")));

            String buff = "";

            while((buff=br.readLine()) != null){
                String [] items = buff.split("[\\s]+");
                if(items.length < 3)continue;



                 if(map.get(items[7]) != null){
                     ArrayList<String> ar = map.get(items[7]);
                     ar.add(buff);
                       map.put(items[7], ar);

                 }      else
                 {
                     ArrayList<String> ar = new ArrayList<String>();
                     ar.add(buff);
                     System.out.println(items[7]);
                     names.add(items[7]);
                       map.put(items[7], ar);
                 }

                


            }

            br.close();

            for(int j=0; j< names.size(); j++){
                String name = names.get(j);
                PrintStream ps = new PrintStream(new FileOutputStream(name + "_out.txt"));

                ArrayList<String> ar = map.get(name);
                for(String s: ar){
                    ps.println(s);
                }

                 ps.close();
            }






        }  catch(Exception ex){
            ex.printStackTrace();

        }


    }

    public static void main(String[] args) {
        new SplitFile();
        }
}

Open in new window

169022-out.txt
169023-out.txt
169024-out.txt
169025-out.txt
169026-out.txt
169027-out.txt
0
 
LVL 47

Accepted Solution

by:
for_yan earned 300 total points
ID: 37726390
This is even simpler varian which does not depend on the size fo the file and can handle files of any size.

Output files are attached.

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

public class SplitFile1 {

       public SplitFile1() {
        try{

            String sep = System.getProperty("line.separator");

            BufferedReader br = new BufferedReader((new FileReader("169022.txt")));
            String curFile = null;
            FileWriter fw = null;

            String buff = "";

            while((buff=br.readLine()) != null){
                String [] items = buff.split("[\\s]+");
                if(items.length < 3)continue;
                String name = items[7];
                if(!name.equals(curFile)){

                    curFile = name;
                    if(fw != null)fw.close();
                    fw = new FileWriter(new File(curFile+ "_out1.txt"), true);
                    

                }
               if(fw != null) fw.write(buff + sep);


            }

            br.close();
            if(fw != null) fw.close();

        } catch(Exception ex){
            ex.printStackTrace();
        }




}

    public static void main(String[] args) {
        new SplitFile1();
    }


}

Open in new window

169022-out1.txt
169023-out1.txt
169024-out1.txt
169025-out1.txt
169026-out1.txt
169027-out1.txt
0
 

Author Closing Comment

by:ClaytonGlass
ID: 37728308
Thank You Very Much!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now