读取Pascal中的注释

本文提供了一个使用Java进行文件读取的示例程序,主要介绍了如何读取文本文件中的内容,并展示了处理文本中注释的基本逻辑。通过该示例,读者可以了解如何利用Java的BufferedReader类来逐行读取文件。
package com.xx;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ReadNotes {

/**
*读取Pascal中的注释
*/
public static void main(String[] args) {
File file = new File("D:/报表.txt");
readFile(file);
}

/**
* 读取文件
* @param file 要读取的文件
*
*/
public static void readFile(File file){
List<String>list = new ArrayList<String>();
BufferedReader readfile = null;
try {
readfile = new BufferedReader(new FileReader(file));
String tempString = null;
int flag = 0;
while((tempString = readfile.readLine()) != null){
if(tempString.indexOf("//") != -1 ){
System.out.println(tempString);
}else if(tempString.indexOf("{") != -1 || flag == 1 && tempString.indexOf("}") == -1){
System.out.println(tempString);
flag = 1;
}else if(tempString.indexOf("}") != -1) {
System.out.println(tempString);
flag = 0;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(readfile != null){
try {
readfile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值