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(); } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步