2 thunder hao Thunder_Hao 于 2016.02.24 12:42 提问

java删除文件内容,删不了?

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Del
{
Del()
{
int sel = 0;
String gzkh = null;
String name = null;
FileReader fr_temp = null;
BufferedReader bufr_temp = null;
FileWriter fw_temp = null;
BufferedWriter bufw_temp = null;

    FileReader fr = null;
    BufferedReader bufr = null;
    FileWriter fw = null;
    BufferedWriter bufw = null;


    @SuppressWarnings("resource")
    Scanner sc =new Scanner(System.in);

    System.out.print("请输入(1/2)确定按工资卡号/姓名删除:");
    sel = sc.nextInt();
    if(sel == 1)
    {
        System.out.print("请输入工资卡号:");
        gzkh = sc.next();
    }
    else if(sel == 2)
    {
        System.out.print("请输入姓名:");
        name = sc.next();
    }

    try
    {
        fr_temp = new FileReader("PayInfo_file.txt");
        bufr_temp = new BufferedReader(fr_temp);
        fw_temp = new FileWriter("temporary.txt");      //临时存储文件
        bufw_temp = new BufferedWriter(fw_temp);


        String line_temp = null;
        while((line_temp = bufr_temp.readLine())!=null) 
        {
            if(line_temp.indexOf(gzkh) != -1||line_temp.indexOf(name) != -1)
                continue;
            bufw_temp.write(line_temp);
            bufw_temp.newLine();    
            bufw_temp.flush();
        }

    } catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }
    catch (IOException e) 
    {
        throw new RuntimeException("读取失败");
    }finally
    {

        try 
        {
            if(bufr_temp!=null)
                bufr_temp.close();
        } catch (IOException e) 
        {
            throw new RuntimeException("读取关闭失败");
        }

        try {
            if(bufw_temp!=null)
                bufw_temp.close();
        } catch (IOException e) {
            throw new RuntimeException("写入关闭失败");
        }
    }


    try {
        fr = new FileReader("temporary.txt");
        bufr = new BufferedReader(fr);
        fw = new FileWriter("PayInfo_file.txt");        //重新打开PayInfo_file文件
        bufw = new BufferedWriter(fw);


        String line = null;
        while((line = bufr.readLine())!=null)   
        {
            bufw.write(line);
            bufw_temp.newLine();    
            bufw_temp.flush();

        }
    } catch (FileNotFoundException e) 
    {
        // TODO 自动生成的 catch 块
        e.printStackTrace();
    } catch (IOException e) 
    {
        throw new RuntimeException("读取失败");
    }finally
    {

        try {
            if(bufr!=null)
                bufr.close();
        } catch (IOException e) 
        {
            throw new RuntimeException("读取关闭失败");
        }

        try {
            if(bufw!=null)
                bufw.close();
        } catch (IOException e) 
        {
            throw new RuntimeException("写入关闭失败");

        }
    }



    System.out.println();

}

}

4个回答

Thunder_Hao
Thunder_Hao   2016.02.24 12:43

请输入选择:3
请输入(1/2)确定按工资卡号/姓名删除:1
请输入工资卡号:101
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.indexOf(Unknown Source)
at java.lang.String.indexOf(Unknown Source)
at Del.(Del.java:54)
at PayInfo_file.main(PayInfo_file.java:29)

Royal_lr
Royal_lr   Ds   Rxr 2016.02.24 12:56

29行空指针错误,,看下怎么为空的

u013196231
u013196231   2016.02.24 13:19

看你代码发现一个问题,scanner不用之后,及时sc.close(),然后FileReader("")中的文件应该是绝对路径FileReader("D:\PayInfo_file.txt");
或者 File file = new File("D:\PayInfo_file.txt"); new FileReader(file);
你先试试,我只是讲了一下我的想法,有不对的地方勿喷啊

u013196231
u013196231 纠错---我上面文件路径写错了,应该是 D:\\PayInfo_file.txt
接近 2 年之前 回复
u013196231
u013196231 纠错---我上面文件路径写错了,应该是 D:\\PayInfo_file.txt
接近 2 年之前 回复
wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2016.02.24 17:58

Del.(Del.java:54)这一行是什么代码呢?那个是用到indexOf的地方看来,猜测应该是

 if(line_temp.indexOf(gzkh) != -1||line_temp.indexOf(name) != -1)

如果是这一行的话,很有可能是你的gzkh或者name为空。检查下这两个变量,你用Scanner 输入时,是否没有输入呢。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!