美文网首页
java操作文件

java操作文件

作者: smallcui | 来源:发表于2017-12-29 15:35 被阅读0次

    优雅读取文件

     List<String> lines = Files.readAllLines(Paths.get("/Users/cong/3.txt"), StandardCharsets
                    .UTF_8);
    

    优雅的写入文件

     PrintStream out = new PrintStream(newFileOutputStream("/Users/cong/Desktop/pass/passwd.txt"));
     out.println(stringBuilder.toString());
    
    

    读取大文件

       try {
    
    
                FileChannel fileChannel = FileChannel.open(Paths.get("/Users/cong/Downloads/crackstation-human-only.txt"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);
    
                MappedByteBuffer mbb = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, 900);
                Charset charset = Charset.forName("utf-8");
                CharsetDecoder decoder = charset.newDecoder();
                CharBuffer charBuffer = decoder.decode(mbb);
                Scanner sc = new Scanner(charBuffer).useDelimiter(System.getProperty("line.separator"));
                while (sc.hasNext()) {
                    String next = sc.next();
                    System.out.println(next);
    
    
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
    

    相关文章

      网友评论

          本文标题:java操作文件

          本文链接:https://www.haomeiwen.com/subject/xunigxtx.html