关于io字符流中删除文本中的行号和注释。

public static void main(String[] args) {
//创建源 仅限于 字符的纯文本
File src =new File("C:\\Users\\Administrator\\Desktop\\test.txt");
File dest =new File("C:\\Users\\Administrator\\Desktop\\test1.txt");
//选择流
BufferedReader reader =null;
BufferedWriter wr =null;
try {
reader =new BufferedReader(new FileReader(src));
wr =new BufferedWriter(new FileWriter(dest));
//新增方法的操作
String line =null;
int num = 0;
while(null!=(line=reader.readLine())){
if(!line.startsWith("//")){//不是双斜杠就执行后面的。
if(num<9){
line = line.substring(1,line.length());//从哪个字符开始,到哪个字符结束。
if(!line.startsWith(" //")){
wr.write(line);
}
}else if(num>8&&num<14){
line = line.substring(2,line.length());
if(!line.startsWith(" //")){
wr.write(line);
}
}else if(num>29&&num<41){
line = line.substring(2,line.length());
if(!line.startsWith(" //")){
wr.write(line);
}
}else{
wr.write(line);
}
num++;
}
wr.newLine(); //换行符号
}
System.out.println(num);
wr.flush();//强制刷出
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("源文件不存在");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取失败");
}finally{
try {
if (null != wr) {
wr.close();
}
} catch (Exception e2) {
}
try {
if (null != reader) {
reader.close();
}
} catch (Exception e2) {
}
}

}



//文本文件

1 importjava.io.*; 

3 public classTestFileStream {
4 public static voidmain(String[] args) throwsIOException {
5 try(
6 // Create an output stream to the file
7 FileOutputStream output = newFileOutputStream("temp.dat");
8 ) {
9 // Output values to the file
10 for(inti = 1; i <= 10; i++)
11 output.write(i);
12 }
13
14 try(
import
output stream
output
Declaring exception in the method
public static voidmain(String[] args)
throwsIOException{
// Perform I/O operations
}
Using try-catch block
public static voidmain(String[] args) {
try{
// Perform I/O operations
}
catch(IOException ex) {
ex.printStackTrace();
}
}
17.4 Binary I/O Classes 683
15 // Create an input stream for the file
16 FileInputStream input = newFileInputStream("temp.dat");
17 ) {
18 // Read values from the file
19 intvalue;
20 while((value = input.read()) != -1)
21 System.out.print(value + " ");
22 }
23 }
24 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值