无法删除Java中的文件

时间:2019-04-10 21:53:39

标签: java delete-file

由于某种原因无法删除和替换文件。

我只想从文件中删除1行。我找到了一种方法,方法是创建一个临时文件,然后逐个写入原始文件中的每一行,但要删除的行除外,然后用临时文件替换原始文件,但是尽管创建了临时文件,但原始文件却无法由于某种原因被删除和替换。我检查了文件未打开。

static final class PositionPriceWrapperBuilder extends RichCoFlatMapFunction<Position, Price, PositionPriceWrapper> {



    private transient MapState<String, Price>  priceState;
    private transient MapState<String, Position>  positionState;        

    @Override
    public void open(Configuration parameters) throws Exception {


        MapStateDescriptor<String, Price>  descPrice = new MapStateDescriptor<String, Price>(
                "priceState",
                String.class,
                Price.class);           
        priceState = getRuntimeContext().getMapState(descPrice);                
        System.out.println("descPrice:: " + descPrice);
        //Same thing needs to be done for Price?


        MapStateDescriptor<String, Position>  descPos = new MapStateDescriptor<String, Position>(
                "positionState",
                String.class,
                Position.class);            
        positionState = getRuntimeContext().getMapState(descPos);               
        System.out.println("positionState:: " + positionState);

    }           

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void flatMap1(Position position, Collector<PositionPriceWrapper> out) throws Exception {
          try {
              //= pnlState.get(position.getId());
              Price price = priceState.get(position.getId());
              PositionPriceWrapper ppw = new PositionPriceWrapper();
              ppw.setPrice(price);
              ppw.setPosition(position);
              ppw.setAccount(position.getAccount());
              ppw.setCusip(position.getCusip());

              System.out.println("Built ppw -->" + ppw);

              positionState.put(position.getId(), position);
              out.collect(ppw);
          }
          catch ( Exception e) {
              e.printStackTrace();
          }

    }

    @Override
    public void flatMap2(Price price, Collector<PositionPriceWrapper> out) throws Exception {
          try {
              Position position = positionState.get(price.getId());
              PositionPriceWrapper ppw = new PositionPriceWrapper();
              ppw.setPrice(price);
              ppw.setPosition(position);
              ppw.setAccount(price.getAccount());
              ppw.setCusip(price.getCusip());

              priceState.put(price.getId(), price);
              out.collect(ppw);
          }
          catch ( Exception e) {
              e.printStackTrace();
          }         

    }

}

1 个答案:

答案 0 :(得分:0)

强烈建议使用java.nio.file.Files#delete(Path)方法而不是File#delete。阅读相关的question。此外,您不必删除文件并写入临时文件。只需阅读文本,根据需要对其进行过滤,最后从头开始重新编写相同的文件即可。 (当然,写操作必须先关闭。)

看看这个例子:

public class ReadWrite {
    public static void main(String[] args) {
        File desktop = new File(System.getProperty("user.home"), "Desktop");
        File txtFile = new File(desktop, "hello.txt");
        StringBuilder sb = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new FileReader(txtFile))) {
            String line;
            while ((line = br.readLine()) != null) {
                if ("this should not be read".equals(line))
                    continue;
                sb.append(line);
                sb.append(System.lineSeparator());
            }
        } catch (IOException e) {
            System.err.println("Error reading file.");
            e.printStackTrace();
        }

        try (PrintWriter out = new PrintWriter(txtFile)) {
            out.write(sb.toString());
        } catch (IOException e) {
            System.err.println("Error writing to file.");
            e.printStackTrace();
        }
    }
}

hello.txt初始内容:

hello there
stackoverflow
this should not be read
but what if it does?
then my answer
is going to get downvotes

运行后:

hello there
stackoverflow
but what if it does?
then my answer
is going to get downvotes

PS :如果您使用的是Java 8+,请查看try-with resourcesAutoClosable.