java基本的文件读写

  1package io;
  2
  3import java.io.*;
  4
  5/**
  6 * @author 
  7 * @version 1.0
  8 */

  9public class ReadLineFromFile
 10{
 11    /**
 12     * Attributes
 13     */

 14    private BufferedReader br;
 15    private String filePath;
 16    
 17    /**
 18     * Constructor
 19     */

 20    public ReadLineFromFile()
 21    {
 22    }

 23    
 24    /**
 25     * Constructor
 26     */

 27    public ReadLineFromFile(String filePath)
 28    {
 29        this.filePath = filePath;
 30    }

 31    
 32    /**
 33     * Method that will set the file path
 34     * 
 35     * @param filePath
 36     */

 37    public void setFilePath(String filePath)
 38    {
 39        this.filePath = filePath;
 40    }

 41    
 42    /**
 43     * Method that will return the file path
 44     * 
 45     * @return string, string that indicates file path
 46     */

 47    public String getFilePath()
 48    {
 49        return this.filePath;
 50    }

 51    
 52    /**
 53     * Method that will open the file
 54     */

 55    public void open() throws Exception
 56    {
 57        FileReader fr = new FileReader(this.filePath);
 58        br = new BufferedReader(fr);
 59    }

 60    
 61    /**
 62     * Method that will open the file with the file path specified
 63     * 
 64     * @param filePath
 65     */

 66    public void open(String filePath) throws Exception
 67    {
 68        FileReader fr = new FileReader(filePath);
 69        br = new BufferedReader(fr);
 70    }

 71    
 72    /**
 73     * Method that will read one line and return the string
 74     * 
 75     * @return String, line read
 76     */

 77    public String readLine() throws Exception
 78    {
 79        return br.readLine();
 80    }

 81    
 82    /**
 83     * Method that will read one byte from the file
 84     * 
 85     * @return int, byte read
 86     */

 87    public int read() throws Exception
 88    {
 89        return br.read();
 90    }

 91    
 92    /**
 93     * Method that will close the file
 94     */

 95    public void close() throws Exception
 96    {
 97        br.close();
 98    }

 99}

100
  1package io;
  2
  3import java.io.*;
  4
  5/**
  6 * 
  7 * @version 1.0
  8 */

  9public class WriteLineToFile
 10{
 11    /**
 12     * Attributes
 13     */

 14    private PrintWriter pw;
 15    private String filePath;
 16    
 17    /**
 18     * Constructor
 19     */

 20    public WriteLineToFile()
 21    {
 22    }

 23    
 24    /**
 25     * Constructor
 26     */

 27    public WriteLineToFile(String filePath)
 28    {
 29        this.filePath = filePath;
 30    }

 31    
 32    /**
 33     * Method that will set the file path
 34     * 
 35     * @param filePath
 36     */

 37    public void setFilePath(String filePath)
 38    {
 39        this.filePath = filePath;
 40    }

 41    
 42    /**
 43     * Method that will return the file path
 44     * 
 45     * @return string
 46     */

 47    public String getFilePath()
 48    {
 49        return this.filePath;
 50    }

 51    
 52    /**
 53     * Method that will open the file
 54     */

 55    public void open() throws Exception
 56    {
 57        FileWriter fw = new FileWriter(this.filePath);
 58        pw = new PrintWriter(fw);
 59    }

 60    
 61    /**
 62     * Method that will open the file and specified if the information should be
 63     * append to the end of the file or not
 64     * 
 65     * @param append
 66     */

 67    public void open(boolean append) throws Exception
 68    {
 69        FileWriter fw = new FileWriter(this.filePath, append);
 70        pw = new PrintWriter(fw);
 71    }

 72    
 73    /**
 74     * Method that will open the file
 75     * 
 76     * @param filePath
 77     */

 78    public void open(String filePath) throws Exception
 79    {
 80        FileWriter fw = new FileWriter(filePath);
 81        pw = new PrintWriter(fw);
 82    }

 83    
 84    /**
 85     * Method that will open the file with the file path specified and if it
 86     * should be append to the end of the file or not
 87     * 
 88     * @param filePath
 89     * @param append
 90     */

 91    public void open(String filePath, boolean append) throws Exception
 92    {
 93        FileWriter fw = new FileWriter(filePath, append);
 94        pw = new PrintWriter(fw);
 95    }

 96    
 97    /**
 98     * Method that will write a line to the file
 99     * 
100     * @param input
101     */

102    public void writeLine(String input)
103    {
104        pw.println(input);
105    }

106    
107    /**
108     * Method that will write a string to the file
109     * 
110     * @param input
111     */

112    public void write(String input)
113    {
114        pw.print(input);
115    }

116    
117    /**
118     * Method that will close the file
119     */

120    public void close()
121    {
122        pw.close();
123    }

124}

125
posted @ 2007-12-03 06:43  N/A2011  阅读(446)  评论(0编辑  收藏  举报