java学习笔记—字节流/字符流/File类操作

       // File常用的属性和方法
	public static void fileTest() throws IOException {
		System.out.println("系统有关的路径分隔符:" + File.pathSeparator);
		System.out.println("系统有关的路径分隔符:" + File.pathSeparatorChar);
		System.out.println("与系统有关的默认名称分隔符:" + File.separator);
		System.out.println("与系统有关的默认名称分隔符:" + File.separatorChar);

		File file = new File("H:\\2T100\\1.txt");
		System.out.println("1.txt文件是否存在:" + file.exists());
		// 如果文件不存在,创建新文件
		if (!file.exists()) {
			file.createNewFile();
		}

		file = new File("H:\\2T100", "1.jpg");
		System.out.println("1.jpg是否可读:" + file.canRead());
		System.out.println("1.jpg是否可写:" + file.canWrite());
		System.out.println("绝对路径 :" + file.getAbsolutePath());
		System.out.println("相对路径 :" + file.getPath());
		System.out.println("文件名:" + file.getName());
		System.out.println("路径:" + file.getParent());
		System.out.println("是否是文件:" + file.isFile());
		System.out.println("是否是目录:" + file.isDirectory());
		System.out.println("文件大小:" + file.length() + "字节");
	}

	// 打印指定文件或目录包括所有子元素
	public static void printFileList(String path, int level) {
		// 构建一个File实例
		File f = new File(path);
		StringBuilder sb = new StringBuilder();
		// 对目录下的子元素进行缩进,显示层次感
		for (int i = 0; i < level; i++) {
			sb.append("\t");
		}
		// 如果文件不为空
		if (f != null) {
			// 如果是目录
			if (f.isDirectory()) {
				// 输出目录名
				System.out.println(sb.toString() + f.getName());
				// 获得目录的子元素的集合
				File[] fileArray = f.listFiles();
				List<File> fileList = sortFile(fileArray);
				if (fileArray != null) {
					// 如果目录不为空,循环得到子元素
					for (int i = 0; i < fileList.size(); i++) {
						printFileList(fileArray[i].getPath(), level + 1); // 递归调用
					}
				}
				// 如果是文件,直接输出文件名
			} else {
				System.out.println(sb.toString() + f.getName());
			}
		}
	}

	// 排序,文件在前,目录在后
	public static List<File> sortFile(File[] files) {
		LinkedList<File> list = new LinkedList<File>();
		for (File file : files) {
			if (file.isFile()) {
				//如果是文件,就放在最前面
				list.addFirst(file);
			} else {
				//如果是目录,就放在最后面
				list.addLast(file);
			}
		}
		return list;
	}

	// 获得指定目录里指定后缀名的文件
	public static void printFileName(String path) {
		File f = new File(path);
		if (f.isDirectory()) {
			//获得子元素的集合
			File[] list = f.listFiles(new JavaFileFilter());
			//遍历集合,输出文件名
			for (File file : list) {
				System.out.println(file.getName());
			}
		}
	}

	// 静态内部类
	static class JavaFileFilter implements FilenameFilter {
		@Override
		public boolean accept(File dir, String name) {
			//检测以.java为后缀的文件
			if (name.endsWith(".java")) {
				return true;
			}
			return false;
		}

	}

	// 字节流读写
	public static void outputStreamTest() {
		File f = new File("d:" + File.separator + "test.txt");
		// 文件存在,进行操作
		if (f.exists()) {
			byte[] b = new byte[1024]; // 字节缓冲区
			int len = -1; // 读取到的字节的个数
			InputStream is = null;
			OutputStream os = null;
			try {
				// 构造输入流
				is = new BufferedInputStream(new FileInputStream(f));
				// 构造输出流
				os = new BufferedOutputStream(new FileOutputStream(new File(
						"d:" + File.separator + "test1.txt")));
				// 读到-1时表示文件结束
				while ((len = is.read(b)) != -1) {
					// 读到len字节就写len个字节
					os.write(b);
					System.out.println("写入" + len + "个字节");
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					//刷新输出流并强制写出所有缓冲的输出字节
					os.flush();
				} catch (IOException e) {
					e.printStackTrace();
				}
				try {
					//关闭输出流
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				try {
					//关闭输入流
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	//字符流读写
	public static void rwTest(){
		int len=-1;
		char[] c=new char[1024]; // 字符缓冲区
		Reader reader=null;
		Writer writer=null;
		try {
			// 构造输入流
			reader=new FileReader(new File("d:"+File.separator+"test.txt"));
			// 构造输出流
			writer=new FileWriter("d:"+File.separator+"1.txt");
			// 读到-1时表示文件结束
			while((len=reader.read(c))!=-1){
				// 读到len字符就写len个字符
				writer.write(c,0,len);
				System.out.println("写入" + len + "个字符");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				//刷新输出流并强制写出所有缓冲的输出字符
				writer.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				//关闭输出流
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				//关闭输入流
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	//字符流整行整行读写
	public static void bufferedRWTest(){
		File f=new File("d:"+File.separator+"test.txt");
		String temp=null; //字符串缓冲区
		BufferedReader br=null;
		BufferedWriter bw=null;
		try {
			// 构造输入流
			br=new BufferedReader(new FileReader(f));
			// 构造输出流
			bw=new BufferedWriter(new FileWriter("d:"+File.separator+"123.txt"));
			// 读到null时表示文件结束
			while((temp=br.readLine())!=null){
				// 读到多少行就写多少行
				bw.write(temp);
				//写入一个行分隔符
				bw.newLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		} finally {
			try {
				//刷新输出流并强制写出所有缓冲的输出字符
				bw.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				//关闭输出流
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				//关闭输入流
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值