该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.io.*;
public class Work2 {
   public static void main(String[] args) throws IOException {
      deleteLine(3);//删除第三行
   }
   private static void deleteLine(int n) throws IOException {
      BufferedReader br = new BufferedReader(new InputStreamReader(
            new FileInputStream("d:\\temp\\books.txt")));
      ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
      PrintWriter pw = new PrintWriter(new OutputStreamWriter(byteArray));
      String strLine;
      for (int i = 1; (strLine = br.readLine()) != null; i++) {
         if (i == n) {
            // do nothing
         } else {
            pw.println(strLine);
         }
      }
      br.close();
      pw.close();
      byte[] bytes = byteArray.toByteArray();
      OutputStream os = new FileOutputStream("d:\\temp\\books.txt");
      for (int i = 0; i < bytes.length; i++) {
         os.write(bytes[i]);
      }
      os.close();
   }
}