zoukankan      html  css  js  c++  java
  • Java file read & write

    1. read

    public static void readfile(String filepath) {
            BufferedReader br = null;
    
            try {
    
                String sCurrentLine;
    
                br = new BufferedReader(new FileReader(filepath));
    
                while ((sCurrentLine = br.readLine()) != null) {
                    System.out.println(sCurrentLine);
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)
                        br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

    2. write

    public static void writefile(String filepath) {
            try {
    
                String content = "This is the content to write into file asndfsa";
    
                File file = new File(filepath);
    
                // if file doesnt exists, then create it
                if (!file.exists()) {
                    file.createNewFile();
                }
    
                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(content);
                bw.close();
    
                System.out.println("Done");
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
  • 相关阅读:
    写了一个Rijndael工具类
    使用bcel动态创建class
    有感于大理古城的天主教堂
    joj 1089 &&zoj 1060&&poj 1094 以及wa的分析和数据
    joj1026
    joj 1317
    joj 1171
    joj 2343
    joj 1078 hdu 1116
    joj 1189
  • 原文地址:https://www.cnblogs.com/phoenix13suns/p/4955122.html
Copyright © 2011-2022 走看看