**
-
FileOutputStream练习 */ public class FileOutputStreamDemo { public static void main(String[] args) {
String path = "D:\\IoDemo\\test2.txt"; //如果文件不存在,则自动创建 //append:是指是否在原有内容后追加,默认为FALSE try(OutputStream os = new FileOutputStream(path)){char c = '赞';int a = c;os.write(a); }catch (FileNotFoundException e) {e.printStackTrace(); }catch (IOException e){e.printStackTrace(); }
} }
/**
-
FileOutputStream练习 */ public class FileOutputStreamDemo2 {
public static void main(String[] args) { String path = "D:\IoDemo\test2.txt"; //如果文件不存在,则自动创建 //append参数为true:在原有内容后追加 try(OutputStream os = new FileOutputStream(path,true)){ String s = "众里寻他千百度,蓦然回首,那人却在,灯火阑珊处。 "; os.write(s.getBytes()); }catch (IOException e){ e.printStackTrace(); } } }
同时使用输入流和输出流来复制文件
/**
-
同时使用FileInputStream和FileOutputStream完成文件的复制操作 */ public class CopyUtilDemo { public static void main(String[] args) { File src = new File("D:\SSH.zip"); File dest = new File("D:\IoDemo\ssh.zip"); copy(src,dest); } public static void copy(File src, File dest){ long start = System.currentTimeMillis(); try(InputStream inputStream = new FileInputStream(src); OutputStream outputStream = new FileOutputStream(dest)) { byte[] bytes = new byte[16384]; int k =0; while((k=inputStream.read(bytes))!=-1){ outputStream.write(bytes); } } catch (IOException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("复制完成!"); System.out.println("共用时"+(end-start)/1000+"秒"); } }
/**
-
解压文件练习 */ public class ZipOutputStreamDemo { public static void main(String[] args) { try{ InputStream inputStream = new FileInputStream("D:\IoDemo\test.zip"); ZipInputStream zipInput = new ZipInputStream(inputStream, Charset.forName("gbk")); ZipEntry zipEntry = null; while ((zipEntry = zipInput.getNextEntry())!=null){ String name = zipEntry.getName(); System.out.print(name); int k = 0; OutputStream outputStream = new FileOutputStream("D:\IoDemo\"+name); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); while((k = zipInput.read())!=-1){ bufferedOutputStream.write(k); } bufferedOutputStream.close(); outputStream.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { throw new RuntimeException(e); } } }
/**
-
压缩文件练习 */ public class ZipInputStreamDemo { public static void main(String[] args) throws IOException { File file = new File("D:\IoDemo\Typora 1.2.3.exe"); FileInputStream fileInputStream = new FileInputStream(file); OutputStream outputStream = new FileOutputStream("D:\IoDemo\typora.zip"); ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); zipOutputStream.putNextEntry(new ZipEntry(file.getName())); int k; while ((k = fileInputStream.read())!=-1){ zipOutputStream.write(k); } zipOutputStream.close(); outputStream.close(); fileInputStream.close(); } } 2、今天没学会什么
学习心得
今天主要学习了I/O流,重点为字节流的输入输出,练习了常用的方法。通过练习更好的掌握主要的知识点,在实践中积累使用经验。
课后练习:
-
遍历D盘,将D盘中所有的路径名和文件名写入文本文件中
/**
-
遍历D盘,将D盘中所有的路径名和文件名写入文本文件中 */ public class IoDemo2 { public static void main(String[] args) { String path = "D:\"; File file = new File(path); ArrayList<String> filenames = new ArrayList<>(); filenames = getPathFileName(file,filenames); generateTxt(filenames); }
/**
-
获取D盘下所有的路径名和文件名
-
@param baseFile */ private static ArrayList<String> getPathFileName(File baseFile,ArrayList<String> fileName) { //File对象的索引子文件和路径 File[] files = baseFile.listFiles(); //在files对象不为Null时遍历 ArrayList<String> filenames = fileName; if (files != null && files.length > 0) { //遍历路径和文件列对象 for (File file : files) { //判断是否是文件 if (file.isFile()) { //调用方法写入 filenames.add(file.getAbsolutePath()); } //判断是否是路径 if (file.isDirectory()) { //如果是路径,需要继续递归调用方法并传递文件对象参数继续遍历输出文件和路径 getPathFileName(file,filenames); }
}
} return filenames; }
private static void generateTxt(ArrayList<String> files){ String path = "D:\IoDemo\filename.txt"; //如果文件不存在,则自动创建 //append参数为true:在原有内容后追加 //资源自动释放 try(OutputStream os = new FileOutputStream(path,true)){ //遍历ArrayList for (int i = 0; i < files.size(); i++) { //将数据写入 os.write(files.get(i).getBytes()); os.write("\r\n".getBytes()); } }catch (IOException e){ e.printStackTrace(); } } }
-