欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java IO操作--读写文本文件

程序员文章站 2022-03-28 16:52:09
...
1、一行一行的读取文本内容
String temp=null;
	        StringBuffer content=new StringBuffer();//StringBuffer类和String一样,也用来代表字符串。区别:偏重于对于字符串的变化,例如追加、插入和删除
	        //下面两种方法都行。
	        BufferedReader read=new BufferedReader(new InputStreamReader(new  FileInputStream("E:/bioasqB/result/1.out")));
	  //    BufferedReader read=new BufferedReader(new FileReader("E:/bioasqB/result/1.out"));
		    while((temp=read.readLine())!=null){
				content.append(temp);
				content.append("\n");
				System.out.println(temp);
			}

2、将整个内容写到文本
public static void FileWrite(String fileName, String content) {   
        FileWriter writer = null;  
        try {     
            // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件     
            writer = new FileWriter(fileName, true);    
            writer = new FileWriter(fileName);//不追加文件内容
            writer.write(content);       
        } catch (IOException e) {     
            e.printStackTrace();     
        } finally {     
            try {     
                if(writer != null){  
                    writer.close();     
                }  
            } catch (IOException e) {     
                e.printStackTrace();     
            }     
        }   
    }

3、将字符串内容循环写到文本中
public String WriteTopic(ArrayList result) throws FileNotFoundException{
		Book book=new Book();
		String id=null;
		String body=null;
		String document=null;
		String qrel=null;
		for(int i=0;i<result.size();i++){
			Pattern p0=Pattern.compile("id=(.*?)bioasq");
			Matcher m0=p0.matcher(result.get(i).toString());
			while(m0.find()){
				id=m0.group(1);
				book.setId(id);
			}
			Pattern p1=Pattern.compile("body:\"(.*?)\"");
			Matcher m1=p1.matcher(result.get(i).toString());
			while(m1.find()){
				body=m1.group(1);
				book.setBody(body);
			}
			Pattern p2=Pattern.compile("\"http://www.ncbi.nlm.nih.gov/pubmed/(.*?)\"");
			Matcher m2=p2.matcher(result.get(i).toString());
			while(m2.find()){
				document=m2.group(1);
	//			book.setDocument(document);
				qrel=id+" "+"0"+" "+document+" "+"1";
				System.out.println(qrel);
				FileOutputStream fos=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\BioASQ_2013_TaskB\\BioASQ_PhaseB.qrel",true);
				PrintStream ps=new PrintStream(fos);
				Scanner in=new Scanner(qrel);
				String s=in.next();
				System.setOut(ps);
		//		System.out.println(s);
			}
		}
		return qrel;
	}

BioASQ_PhaseB.qrel格式
1 0 23220349 1
1 0 19582169 1
1 0 22161322 1
1 0 18025684 1
1 0 15701682 1
1 0 15215406 1
1 0 18081932 1
1 0 18629289 1
1 0 21729286 1
1 0 11438739 1
1 0 10573422 1
2 0 22975810 1
2 0 22852678 1
2 0 22278059 1
2 0 21500720 1
2 0 23331310 1
2 0 23250067 1
2 0 23003992 1
2 0 22714377 1
2 0 22665786 1
2 0 22653729 1
2 0 22330507 1
2 0 22286383 1
2 0 21293374 1
3 0 22921312 1
3 0 22480152 1
3 0 22315491 1
3 0 22258533 1
3 0 21415082 1
3 0 21068339 1
3 0 20569258 1
4 0 23297037 1
4 0 22987359 1
4 0 22540951 1
4 0 22247276 1
......
相关标签: io 文本