Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. import java.io.FileReader;
  2. import java.io.BufferedReader;
  3. import java.io.*;
  4. import java.util.ArrayList;
  5. import java.util.*;
  6.  
  7. public class FormatFile {
  8.  
  9.     public static void main(String[] args)
  10.     {
  11.         try {
  12.             String filename= "out.txt";
  13.             FileWriter fw = new FileWriter(filename);
  14.             fw.write("");
  15.             fw.close();
  16.         }
  17.         catch (Exception e){System.err.println("" + e);}
  18.  
  19.         // get data from args
  20.         String filename = "testFile.txt";
  21.  
  22.         // file IO
  23.         BufferedReader reader;
  24.         String line;
  25.         String[] tokens;
  26.  
  27.         // try/catch because file IO
  28.         try
  29.         {
  30.             reader = new BufferedReader(new FileReader(filename)); //set up I/O
  31.  
  32.             // MAIN LOOP: this is for EACH line you read in
  33.             while ((line = reader.readLine()) != null)
  34.             {
  35.                 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
  36.                 tokens = splitClass[1].split(" "); //split the right side by spaces
  37.                 Output(tokens, splitClass); //output to file method
  38.             }
  39.         }
  40.         catch (Exception e){System.err.println("" + e);}
  41.     }
  42.  
  43.     private static void Output(String[] stringArray, String[] stringArray2)
  44.     {
  45.  
  46.         String text = stringArray2[0] + "\t"; //add class and tab
  47.         int i;
  48.  
  49.         for(String t : stringArray)
  50.         {
  51.             if (t.length() >= 3) //if a words string length is bigger than 2
  52.                 text += t + " ";
  53.         }
  54.         text += "\n"; //new line on end
  55.  
  56.         try {
  57.             String filename= "out.txt";
  58.             FileWriter fw = new FileWriter(filename, true);
  59.             fw.write(text);
  60.             fw.close();
  61.         }
  62.         catch (Exception e){System.err.println("" + e);}
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement