黑马程序员技术交流社区

标题: IO流Copy的行数问题 [打印本页]

作者: 张建峰    时间: 2012-8-13 00:02
标题: IO流Copy的行数问题
[code]
public static void main(String[] args) throws IOException {
  FileInputStream fi= new FileInputStream("Input.txt");
  FileOutputStream fo= new FileOutputStream("Output.txt");
  int f;
  while((f=fi.read())!=-1)
   fo.write(f);
  fi.close();
  fo.close();
  
}
如果说Input.txt文件中有7行文字,我怎么只把前三行Copy到Output.txt中
是要判断字符是不是\n吗,如果是,那么\n代表几个字符,如果第一行的文字中有\n的话,怎么办
作者: 官文昌    时间: 2012-8-13 00:21
你再运行下看看~~我把你的呃代码复制过来~~是全部复制了的啊~~~
作者: 官文昌    时间: 2012-8-13 00:21
本帖最后由 官文昌 于 2012-8-13 00:23 编辑

你再运行下看看~~我把你的代码拿来运行了~~是全部复制了的啊~~~
作者: 马林康    时间: 2012-8-13 00:32
  1. public class Test {
  2.         public static void main(String[] args) throws IOException{
  3.                 BufferedWriter bw = new BufferedWriter(new FileWriter("Output.txt"));
  4.                 BufferedReader br = new BufferedReader(new FileReader("Input.txt"));
  5.                 for(int x=0;x<3;x++){
  6.                         String line = br.readLine();
  7.                         bw.write(line);
  8.                         bw.newLine();
  9.                 }
  10.                 br.close();
  11.                 bw.close();
  12.         }
  13. }
复制代码
读取字符 干嘛要用字节流 ??




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2