首页 > 代码库 > oop.io流的应用
oop.io流的应用
io流分为 字节流与字符流
字节流读写的是字节二进制 使用于 图片、视频
字节输入流 inputstream 实现类 FileInputstream
字节输出流 outputstream 实现类 Fileoutputstream
inputstream 子类fileinputstream 子类datainputstream与dataoutputstream
字符流读写的是字符文本 实用于文本的读写
字符输入流 reader 实现类 FileReader
字符输出流 writer 实现类 FileWriter
带缓存区的字符流适合大文本读取,按一行一行的读取 bufferadreader 和bufferadwriter
package oop.io.file; import java.io.File; import java.io.IOException; public class FileDemo { /** * 创建文件夹 */ public static void createFile(File file) { /** * 判断是否有文件 */ if (file.exists()) { if (file.isFile()) { System.out.println("是文件"); } else if (file.isDirectory()) { System.out.println("是文件夹"); } } else /** * 没有文件夹 */ { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } /** * 查看文件家 * */ public static void getFileInfo(File file) { System.out.println("文件名" + file.getName()); System.out.println("文件大小" + file.length()); System.out.println("文件相对路径" + file.getPath()); System.out.println("文件绝对路径" + file.getAbsolutePath()); } /** * 删除文件 * */ public static void deleteFile(File file){ if(file.exists()){ if (file.isFile()) { file.delete(); System.out.println("文件已删除"); } }else{ System.out.println("文件不存在"); } } }
package oop.io.file; import java.io.File; public class Test { public static void main(String[] args) { File file=new File("D://holly.txt"); FileDemo.createFile(file); FileDemo.getFileInfo(file); //FileDemo.deleteFile(file); } }
package oop.io.inoutstram; 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; import oop.io.file.FileDemo; public class InOutStream { public static void main(String[] args) throws IOException { File file=new File("D://bizhi.jpg"); /*readerFile(file); writeFile(file);*/ File file2=new File("D://ouchanglu.jpg"); FileDemo.createFile(file2); copyPater(file2, file); } /** * 文件复制粘贴 */ /** * 字节输入流,写的 * @throws IOException * */ public static void copyPater(File file2,File file) throws IOException{ InputStream inputStream=new FileInputStream(file); byte[] bsw=new byte[(int) file.length()]; inputStream.read(bsw); OutputStream outputstream=new FileOutputStream(file2); outputstream.write(bsw); inputStream.close(); outputstream.close(); } public static void writeFile(File file) throws IOException{ //创建输入流 OutputStream outputStream=new FileOutputStream(file); String str="abcdefghijklmnopqrst"; byte[] bsw=str.getBytes(); outputStream.write(bsw); outputStream.close(); } /** * * * 读取文件流 */ public static void readerFile(File file) throws IOException{ InputStream inputStream=new FileInputStream(file); byte[] bws=new byte[(int) file.length()]; inputStream.read(bws); for (byte b : bws) { System.out.print(b); } inputStream.close(); } }
package oop.io.readerwrite; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; public class ReadQWrite { public static void main(String[] args) throws IOException { copy("D://holly.txt", "D://ouchanglu.txt"); } /** * 复制粘贴 * @throws IOException */ public static void copy(String path2,String path) throws IOException{ Reader reader=new FileReader(path2); char[] cs=new char[path2.length()]; reader.read(cs); Writer writer=new FileWriter(path); writer.write(cs); reader.close(); writer.close(); } }
package oop.io.readerwrite; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; public class ReadQWrite { public static void main(String[] args) throws IOException { copy("D://holly.txt", "D://ouchanglu.txt"); } /** * 复制粘贴 * @throws IOException */ public static void copy(String path2,String path) throws IOException{ Reader reader=new FileReader(path2); char[] cs=new char[path2.length()]; reader.read(cs); Writer writer=new FileWriter(path); writer.write(cs); reader.close(); writer.close(); } }
package oop.io.readerwrite; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; public class ReadQWrite { public static void main(String[] args) throws IOException { copy("D://holly.txt", "D://ouchanglu.txt"); } /** * 复制粘贴 * @throws IOException */ public static void copy(String path2,String path) throws IOException{ Reader reader=new FileReader(path2); char[] cs=new char[path2.length()]; reader.read(cs); Writer writer=new FileWriter(path); writer.write(cs); reader.close(); writer.close(); } }
oop.io流的应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。