Thunder_Hao
Thunder_Hao
2016-02-24 04:42

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

  • 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条回答

为你推荐

换一换