Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileReader;
- import java.io.BufferedReader;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.*;
- public class FormatFile {
- public static void main(String[] args)
- {
- try {
- String filename= "out.txt";
- FileWriter fw = new FileWriter(filename);
- fw.write("");
- fw.close();
- }
- catch (Exception e){System.err.println("" + e);}
- // get data from args
- String filename = "testFile.txt";
- // file IO
- BufferedReader reader;
- String line;
- String[] tokens;
- // try/catch because file IO
- try
- {
- reader = new BufferedReader(new FileReader(filename)); //set up I/O
- // MAIN LOOP: this is for EACH line you read in
- while ((line = reader.readLine()) != null)
- {
- String[] splitClass = line.split("\t"); //split the line by tab, so splitClass[0] is things left of tab, [1] is things to the right aka the rest of the line
- tokens = splitClass[1].split(" "); //split the right side by spaces
- Output(tokens, splitClass); //output to file method
- }
- }
- catch (Exception e){System.err.println("" + e);}
- }
- private static void Output(String[] stringArray, String[] stringArray2)
- {
- String text = stringArray2[0] + "\t"; //add class and tab
- int i;
- for(String t : stringArray)
- {
- if (t.length() >= 3) //if a words string length is bigger than 2
- text += t + " ";
- }
- text += "\n"; //new line on end
- try {
- String filename= "out.txt";
- FileWriter fw = new FileWriter(filename, true);
- fw.write(text);
- fw.close();
- }
- catch (Exception e){System.err.println("" + e);}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement