zoukankan      html  css  js  c++  java
  • Java文件读写

    import java.io.*;
    
    public class Study {
        public static void main(String[] args) {
            try {
                String strPath = "d:\myTest\abc.txt";
                File txtFile = new File(strPath);
                if( !txtFile.exists() ){
                    System.out.println("No found text file.");
                    System.out.println(txtFile.getPath());
                    System.out.println(txtFile.getName());
                    System.out.println(txtFile.getParentFile().getPath());
                    boolean bMakeDir = txtFile.getParentFile().mkdir();
                    System.out.println(bMakeDir);
                    System.out.println(txtFile.createNewFile());
                }
                else{
                    System.out.println("Yes, the text file already existing.");
                }
                
                String strDir = "D:\myTest";
                File dir = new File(strDir);
                System.out.println(dir.isFile());
                System.out.println(dir.isDirectory());
                
                FileWriter fw = new FileWriter(txtFile);
                fw.write("abcdefg");
                fw.close();
                
                String txt;
                StringBuilder text = new StringBuilder();
                BufferedReader br = new BufferedReader(new FileReader(txtFile));
                while((txt = br.readLine()) != null){
                    text.append(txt);
                }
                System.out.println(text);
                
                txtFile.deleteOnExit();
            }
            catch( IOException ex ){    
                ex.printStackTrace();
            }
        }
    }
  • 相关阅读:
    为django项目创建虚拟环境
    linux下安装python
    使用scrapy-crawlSpider 爬取tencent 招聘
    linux基础3
    Scrapy
    scrapy-Redis 分布式爬虫
    scrapy-redis(一)
    Linux中文件上传使用rz
    centos 7 安装nginx
    MySQL 5.7 zip 文件安装过程
  • 原文地址:https://www.cnblogs.com/HD/p/3623188.html
Copyright © 2011-2022 走看看