Java IO实战操作(二)
2012-04-29 22:29
417 查看
/** * 向文件中追加新内容 * @throws IOException */ public void NewInserNum() throws IOException { String fileName = "D:" + File.separator + "IO.txt"; File file = new File(fileName); OutputStream out = new FileOutputStream(file, true); String str = "我在成都名流学习SoftWare"; byte[] Bbyte = str.getBytes(); for (int i = 0; i < Bbyte.length; i++) { out.write(Bbyte[i]); } out.close(); } /** * 获取准确大小空间读取文件 * @throws IOException */ public void SelectFile() throws IOException { String fileName = "D:" + File.separator + "IO.txt"; File file = new File(fileName); InputStream in = new FileInputStream(file); byte[] Bbyte = new byte[(int) file.length()]; in.read(Bbyte); System.out.println("文件长度为:" + file.length()); in.close(); System.out.println(new String(Bbyte)); } /** * 一个一个的读 */ public void OneSelectFile() throws IOException { String fileName = "D:" + File.separator + "hello.txt"; File f = new File(fileName); InputStream in = new FileInputStream(f); byte[] b = new byte[(int) f.length()]; for (int i = 0; i < b.length; i++) { b[i] = (byte) in.read(); } in.close(); System.out.println(new String(b)); } /** * 不知道文件有多大的情况下读取文件 */ public void NotSelectFile() throws IOException{ String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); InputStream in=new FileInputStream(f); byte[] b=new byte[1024]; int count =0; int temp=0; while((temp=in.read())!=(-1)){ b[count++]=(byte)temp; } in.close(); System.out.println(new String(b)); } /** * 字符流 * @throws IOException */ public void StringFile() throws IOException{ String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); Writer out =new FileWriter(f); String str="hello"; out.write(str); out.close(); } /** * 字符流 * 从文件中读出内容 */ public void OutStringFile() throws IOException{ String fileName="D:"+File.separator+"hello.txt"; File f=new File(fileName); char[] ch=new char[100]; Reader read=new FileReader(f); int temp=0; int count=0; while((temp=read.read())!=(-1)){ ch[count++]=(char)temp; } read.close(); System.out.println("内容为"+new String(ch,0,count)); } /** * 文件复制 * @throws IOException */ public void CoutStringFile(String[]args) throws IOException{ if(args.length!=2){ System.out.println("命令行参数输入有误,请检查"); System.exit(1); } File file1=new File(args[0]); File file2=new File(args[1]); if(!file1.exists()){ System.out.println("被复制的文件不存在"); System.exit(1); } InputStream input=new FileInputStream(file1); OutputStream output=new FileOutputStream(file2); if((input!=null)&&(output!=null)){ int temp=0; while((temp=input.read())!=(-1)){ output.write(temp); } } input.close(); output.close(); } /** * 将字节输出流转化为字符输出流 * @throws IOException */ public void OutputStream() throws IOException{ String fileName= "d:"+File.separator+"hello.txt"; File file=new File(fileName); Writer out=new OutputStreamWriter(new FileOutputStream(file)); out.write("hello"); out.close(); } /** * 将字节输入流变为字符输入流 * @throws IOException */ public void InputStream() throws IOException{ String fileName= "d:"+File.separator+"hello.txt"; File file=new File(fileName); Reader read=new InputStreamReader(new FileInputStream(file)); char[] b=new char[100]; int len=read.read(b); System.out.println(new String(b,0,len)); read.close(); } /** * 使用内存操作流将一个大写字母转化为小写字母 * @throws IOException */ public void ByteArray() throws IOException{ String str="ROLLENHOLT"; ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes()); ByteArrayOutputStream output=new ByteArrayOutputStream(); int temp=0; while((temp=input.read())!=-1){ char ch=(char)temp; output.write(Character.toLowerCase(ch)); } String outStr=output.toString(); input.close(); output.close(); System.out.println(outStr); }
相关文章推荐
- Java IO实战操作(三)
- Java IO实战操作(三)
- Java IO实战操作(四)
- Java IO实战操作(四)
- Java IO实战操作(一)
- Java IO实战操作(一)
- Java IO实战操作(二)
- java IO总结之字节流操作文件读写
- Java IO操作——JAVA输入数据操作实例
- java的io操作
- java下的IO操作
- Java学习笔记——File类文件管理及IO读写、复制操作
- java IO操作:字节流与字符流区别
- POI实战-java开发excel详解(第四章 常用操作-单元格合并与数据读取)
- JAVA IO操作
- Java中的IO操作
- 【跟着imooc重学java】Java File IO读写操作
- 84、java的IO操作-转化流
- Java基础 4000 之基本IO操作
- Java学习笔记---IO操作