Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 1.91 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4. import java.util.Random;
  5.  
  6. public class FileIO {
  7.  
  8.         public static void main(String[] args){
  9.                
  10.                 Scanner scan = new Scanner(System.in);
  11.                 String fileName;
  12.                
  13.                 System.out.print("Enter file name: ");
  14.                 fileName = scan.nextLine();
  15.                
  16.                 File f1 = new File(fileName);
  17.                 writeFile(f1);
  18.                 printFile(f1);
  19.                 squareFile(f1);
  20.                
  21.         }
  22.        
  23.         private static void squareFile(File f) {
  24.  
  25.                 try{
  26.                        
  27.                         BufferedReader in = new BufferedReader(new FileReader(f));;
  28.                         String line = "";
  29.                         ArrayList<String> words = new ArrayList<String>();
  30.                         ArrayList<Integer> numbers = new ArrayList<Integer>();
  31.                         String[] temp;
  32.                        
  33.                         while((line = in.readLine()) != null){
  34.                                
  35.                                 temp = line.split(" ");
  36.                                 words.add(temp[0]);
  37.                                 numbers.add(Integer.parseInt(temp[1]));
  38.                                
  39.                         }
  40.                
  41.                 }catch(Exception e){
  42.                        
  43.                        
  44.                 }
  45.                
  46.         }
  47.  
  48.         private static void printFile(File f) {
  49.                
  50.                 try{
  51.                        
  52.                         BufferedReader in = new BufferedReader(new FileReader(f));;
  53.                         String line = "";
  54.                        
  55.                         while((line = in.readLine()) != null){
  56.                                
  57.                                 System.out.println(line);
  58.                                
  59.                         }
  60.                
  61.                 }catch(Exception e){
  62.                        
  63.                        
  64.                 }
  65.  
  66.                
  67.         }
  68.  
  69.         private static void writeFile(File f){
  70.                
  71.                 Random rand = new Random();
  72.                 int r;
  73.                
  74.                 try{
  75.                        
  76.                         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
  77.                         r = rand.nextInt(11) + 10; //gives range [10,20], inclusive;
  78.                        
  79.                         for(int i = 0; i < r; i++){
  80.                                
  81.                                 out.println(getRandomString() + " " + (rand.nextInt(10) + 1));
  82.                                
  83.                         }
  84.                        
  85.                         out.close();
  86.                  
  87.                 }catch(Exception e){
  88.                        
  89.                        
  90.                 }
  91.                
  92.                
  93.         }
  94.  
  95.         private static String getRandomString() {
  96.  
  97.                 String ret = "";
  98.                 Random rand = new Random();
  99.                
  100.                 for(int i = 0; i < 3; i++){
  101.                        
  102.                         ret += (char) (rand.nextInt(26) + 65); //gives an int in the range [65,90], which is uppercase letters in ASCII
  103.                        
  104.                 }
  105.                
  106.                 return ret;
  107.                
  108.         }
  109.        
  110. }