在Java中读写同一个文件 [英] Reading and writing from and to the same file in Java

查看:519
本文介绍了在Java中读写同一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 criteria.txt 文件中读取,以便在同一个文件的结尾标记和附加标记。程序抛出一个异常:找不到文件!我不知道我的错误在哪里。任何建议会帮助我。

以下是我的代码:

  import java.io. *; 
import java.util.StringTokenizer;

public class Test
{
private FileReader fr;
私有BufferedReader br;
private FileWriter fw;
私人BufferedWriter bw;
私人StringTokenizer strtok;
private String s;
$ b //构造函数
public Test()
{
try
{
fw = new FileWriter(criteria.txt,true );
bw = new BufferedWriter(fw);

尝试
{
fr = new FileReader(criteria.txt);
br = new BufferedReader(fr); $($ s


while(strtok.hasMoreTokens())
{
bw.write(\\\
+ strtok.nextToken());
}
br.close();

$ b catch(FileNotFoundException e)
{
System.out.println(File was not found!);

catch(IOException e)
{
System.out.println(No file found!);
}

bw.close();

catch(FileNotFoundException e)
{
System.out.println(Error1!);

catch(IOException e)
{
System.out.println(Error2!);


$ b $ public static void main(String [] args)
{
Test t = new Test();



$ div $解析方案

在完成读取文件之后,即在while循环之后,需要关闭阅读器。目前,您在阅读第一行后会关闭它,当它尝试读取第二行时,会导致 IOException:Stream closed >。

改为:

  while((s = br.readLine())!= null) 
{
strtok = new StringTokenizer(s,);
while(strtok.hasMoreTokens())
{
bw.write(\\\
+ strtok.nextToken());
}
//br.close(); < -----将它移到while循环之外
}
br.close();


I want to read from criteria.txt file, to tokenize and append at the end of the same file the tokens. The program throws an exception: No file found! I do not know where my mistake is. Any suggestion would help me. Thank you in advance!

Here is my code:

import java.io.*;
import java.util.StringTokenizer;

public class Test
{
    private FileReader fr;
    private BufferedReader br;
    private FileWriter fw;
    private BufferedWriter bw;
    private StringTokenizer strtok;
    private String s;

    //constructor
    public Test()  
    {
        try
        {
            fw=new FileWriter("criteria.txt", true);
            bw=new BufferedWriter(fw);

            try
            {
                fr=new FileReader("criteria.txt");
                br=new BufferedReader(fr);

                while((s=br.readLine())!=null)
                {
                    strtok=new StringTokenizer(s," ");
                    while(strtok.hasMoreTokens())
                    {
                        bw.write("\n"+strtok.nextToken());
                    }
                    br.close();
                }
            }
            catch(FileNotFoundException e)
            {
                System.out.println("File was not found!");
            }
            catch(IOException e)    
            {
                System.out.println("No file found!");
            }

            bw.close();
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Error1!");
        }
        catch(IOException e)    
        {
            System.out.println("Error2!");
        }
    }   

    public static void main(String[] args) 
    {
        Test t=new Test();
    }
}

解决方案

You need to close your reader after you have finished reading the file i.e. after the while loop. Currently, you are closing it after reading the first line, which causes an "IOException: Stream closed" when it tries to read the second line.

Change to:

while((s=br.readLine())!=null)
{
    strtok=new StringTokenizer(s," ");
    while(strtok.hasMoreTokens())
    {
        bw.write("\n"+strtok.nextToken());
    }
    //br.close(); <----- move this to outside the while loop
}
br.close();

这篇关于在Java中读写同一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆