Advertisement
wingman007

Java_TextFileReadWrite

Nov 23rd, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package readwrite;
  8.  
  9. import java.io.IOException;
  10. import java.io.FileReader;
  11. import java.io.BufferedReader;
  12. import java.io.FileWriter;
  13. import java.io.PrintWriter;
  14.  
  15. /**
  16.  *
  17.  * @author fmi
  18.  */
  19. public class ReadWrite {
  20.  
  21.     /**
  22.      * @param args the command line arguments
  23.      */
  24.     public static void main(String[] args) {
  25.         // TODO code application logic here
  26.         try
  27.         {
  28.             writeToFile("C:\\Users\\fmi\\test.txt", "Another line", true );
  29.            
  30.            
  31.             String[] result = readFile("C:\\Users\\fmi\\test.txt");
  32.             for (String row : result) {
  33.                 System.out.println(row);
  34.             }
  35.            
  36.            
  37.         }
  38.         catch(IOException e)
  39.         {
  40.             System.out.println(e.getMessage());
  41.         }
  42.     }
  43.    
  44.     public static String[] readFile(String path)  throws IOException
  45.     {
  46.         // String path = "c:\\test.txt";
  47.         // This reads bytes from a text file, and each byte is a single character
  48.         FileReader fr = new FileReader(path);
  49.  
  50.         // You can read whole lines of text, rather than single characters.
  51.         // To do this, you can hand your FileReader over to something called a BufferedReader
  52.         BufferedReader textReader = new BufferedReader(fr);
  53.  
  54.         // first variant
  55.         // int numberOfLines = 3; // hard code the number of lines
  56.         // int numberOfLines = 3; // readLines();
  57.         int numberOfLines = readLines(path);
  58.         String[ ] textData = new String[numberOfLines];
  59.  
  60.         int i;
  61.  
  62.         for (i=0; i < numberOfLines; i++) {
  63.             textData[ i ] = textReader.readLine();
  64.         }
  65.  
  66.         textReader.close( );
  67.        
  68.         return textData;
  69.     }
  70.    
  71.     public static int readLines(String path) throws IOException {
  72.         // This reads bytes from a text file, and each byte is a single character
  73.         FileReader fr = new FileReader(path);      
  74.         BufferedReader textReader = new BufferedReader(fr);
  75.  
  76.         String aLine;
  77.         int numberOfLines = 0;
  78.  
  79.         while ((aLine = textReader.readLine()) != null) {
  80.                 numberOfLines++;
  81.         }
  82.  
  83.         textReader.close();
  84.  
  85.         return numberOfLines;
  86.     }      
  87.    
  88.     public static void writeToFile(String path, String textLine, Boolean appendToFile ) throws IOException {
  89.         // The FileWriter write bytString pathes
  90.         FileWriter write = new FileWriter( path , appendToFile);
  91.         // But we can hand the FileWriter plain text with the aid of the PrintWriter class
  92.         PrintWriter printLine = new PrintWriter( write );
  93.  
  94.         printLine.printf( "%s" + "%n" , textLine);
  95.  
  96.         // This line closes the text file and frees up any resources it was using
  97.         printLine.close( );
  98.     }    
  99.    
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement