【自动化__持续集成】___java___文本文件__字节流
一、代码如下
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 | package www.wujianbotwo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class Demo09 { public static void main(String[] args) { // TODO Auto-generated method stub Demo09 demo09= new Demo09(); demo09.byteRead(); } private void byteRead() { try { File file= new File( "D:\\Test.txt" ); InputStream in= new FileInputStream(file); byte [] buf= new byte [( int )file.length()]; in.read(buf); in.close(); for ( byte b: buf) { System.out.println(b); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
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 | package www.wujianbotwo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Demo09 { public static void main(String[] args) { // TODO Auto-generated method stub Demo09 demo09= new Demo09(); //demo09.byteRead(); demo09.byteWrite(); demo09.byteWriteFalse(); } private void byteRead() { try { File file= new File( "D:\\Test.txt" ); InputStream in= new FileInputStream(file); byte [] buf= new byte [( int )file.length()]; in.read(buf); in.close(); for ( byte b: buf) { System.out.println(b); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void byteWrite() { try { File file= new File( "D:\\Test.txt" ); OutputStream out= new FileOutputStream(file, true ); String content= "这是文件内容。\r\n" ; byte [] buf= content.getBytes(); out.write(buf, 0 , buf.length); out.close(); System.out.println( "输入成功!!" ); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void byteWriteFalse() { try { File file= new File( "D:\\Test.txt" ); OutputStream out= new FileOutputStream(file, false ); String content= "这是文件内容。\r\n" ; byte [] buf= content.getBytes(); out.write(buf, 0 , buf.length); out.close(); System.out.println( "输入成功!!" ); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步