package file;import java.io.*;/*** io文件操作** @author ZwT* @date 2018年05月21日 15:33**/public class IOFile {public void writeAndRead(){/*创建目录和文件以及写入内容*/File file = new File("tmp");/*创建tmp目录*/file.mkdir();if(file.isDirectory()){System.out.println(file.getName()+"目录创建成功");File file1 = new File(file.getPath()+"/zwt.text");if(!file1.exists()){try {file1.createNewFile();System.out.println(file1.getName()+"文件创建成功");/*写入内容*/OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file1.getPath()),"UTF-8");BufferedWriter bw = new BufferedWriter(osw);bw.write("我爱你!\r\n");bw.write("夏梅!");/*关闭流*/bw.close();osw.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();System.out.println(file1.getName()+"文件创建失败");}}}/*读取目录和文件和内容*/File file1 = new File(file.getPath());/*判断是否是目录*/if(file1.isDirectory()){System.out.println(file.getName()+"是一个目录");File[] files = file1.listFiles();for(File tmpFile : files){/*判断是否是文件*/if(tmpFile.isFile()){System.out.println(tmpFile.getName()+"是一个文件");/*读取文件内容*/try {InputStreamReader read = new InputStreamReader(new FileInputStream(tmpFile),"UTF-8");BufferedReader br = new BufferedReader(read);StringBuffer content=new StringBuffer("");String line;int len = 0;while ((line = br.readLine())!=null){if(len == 0){content.append(line);}else{content.append("\r\n"+line);}len++;}read.close();br.close();System.out.println(content);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}}else{System.out.println(file.getName()+"不是一个目录");}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。