Java io 操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | package tlistpackage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * @author Administrator * */ public class listdemo { public static void main(String[] args) throws IOException { // System.out.println(Runtime.getRuntime().getClass()); // System.out.println(Math.round(23.55)); // System.out.println(Math.ceil(26.333)); // System.out.println((int)(Math.random()*10+1)); // Date d=new Date(); // System.out.println(d.toString()); // //格式化时间 // System.out.println(DateFormat.getDateInstance().format(d)); // char[] buffer=new char[1024]; // int len=0; // while((len=rf.read(buffer))!=-1) // { // System.out.print(new String(buffer,0,len)); // } // WriteFile("txtdemo.txt","dirk.wang"); // String textString= ReadFile("txtdemo.txt"); // System.out.print(textString); // InputWriteFile("txtdemo.txt","dirk.wang,show time"); // String str= InputReadFile("txtdemo.txt"); // System.out.print(str); CopyFile( "txtdemo.txt" , "new.txt" ); } /** * @param path 路径 * @return */ @SuppressWarnings ( "finally" ) public static String ReadFile(String path) { FileReader fs= null ; String str= "" ; try { fs= new FileReader(path); char [] buffer = new char [ 1024 ]; int len= 0 ; while ((len=fs.read(buffer))!=- 1 ) { str+= new String(buffer, 0 ,len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fs!= null ) { try { fs.close(); } catch (IOException e2) { e2.printStackTrace(); } } return str; } } /** * @param path 路径 * @param text 写入的内容 */ public static void WriteFile(String path,String text) { FileWriter fw = null ; try { //true 表示追加写入文件 fw= new FileWriter(path, true ); fw.write(text); //fw.write(text); fw.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { if (fw!= null ) { try { fw.close(); } catch (IOException e2) { // TODO: handle exception e2.printStackTrace(); } } } } /** * 通过二进制流读取文件 * @param path 路径 * @return */ public static String InputReadFile(String path) { File file= new File(path); StringBuilder str= new StringBuilder(); if (file.isFile()) { FileInputStream fs= null ; try { fs= new FileInputStream(file); byte [] buffer= new byte [ 1024 ]; int len= 0 ; while ((len=fs.read(buffer))!=- 1 ) { str.append( new String(buffer, 0 ,len)); } } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { if (fs!= null ) { try { fs.close(); } catch (IOException e2) { // TODO: handle exception e2.printStackTrace(); } } } } return str.toString(); } /** * @param path 路径 * @param text 内容 */ public static void InputWriteFile(String path,String text) { File file= new File(path); FileOutputStream fos= null ; if (file.isFile()) { try { fos= new FileOutputStream(file, true ); fos.write(text.getBytes()); fos.flush(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { if (fos!= null ) { try { fos.close(); } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } } } } } public static void CopyFile(String oldPath,String newPath) { FileInputStream fis= null ; FileOutputStream fos= null ; try { fis= new FileInputStream(oldPath); fos= new FileOutputStream(newPath); int len= 0 ; byte [] buffer = new byte [ 1024 ]; while ((len=fis.read(buffer))!=- 1 ) { fos.write(buffer, 0 ,len); fos.flush(); } } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { try { fis.close(); fos.close(); } catch (IOException e2) { // TODO: handle exception e2.printStackTrace(); } } } } |
本文作者:WangJunZzz
本文链接:https://www.cnblogs.com/WangJunZzz/p/7788368.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义