java I/O

/**
 * @author qinglong
 *  java 输入输出实例
 */
public class InputOrOutput {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// test_byte_array_stream();
		// if (test_buffer_read("f:\\testdata\\11.txt", "f:/testdata/22.txt")) {
		// System.out.println("数据已经成功写入");
		// } else {
		// System.out.println("数据写入或读取失败");
		// }
		//test_data_stream();
		copyFile("f:\\testdata","e:\\");
	}

	/**
	 * 将文件读入输入流中。。然后在改变其中的某值 再写回文件
	 * 
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public static boolean test_char_array_stream(String filename)
			throws IOException {
		File file = new File(filename);

		BufferedReader bufInputReader = new BufferedReader(new FileReader(file));

		// 将文件读入字符数组
		CharArrayWriter charArrayWriter = new CharArrayWriter();

		char[] array = new char[1];
		while (bufInputReader.read(array) != -1) {
			charArrayWriter.write(array);
		}

		charArrayWriter.close();
		bufInputReader.close();

		// 显示字符内容
		array = charArrayWriter.toCharArray();
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + " ");
		}
		System.out.println();

		// 让使用者输入位置与字符修改字符数组内容
		Scanner scanner = new Scanner(System.in);

		System.out.println("输入修改位置");
		int pos = scanner.nextInt();
		System.out.println("输入修改字符");
		char ch = scanner.next().charAt(0);
		array[pos - 1] = ch;

		// 将字符数组内容存回文件
		CharArrayReader charArrayReader = new CharArrayReader(array);

		BufferedWriter bufWriter = new BufferedWriter(new FileWriter(file));

		char[] tmp = new char[1];
		while (charArrayReader.read(tmp) != -1) {
			bufWriter.write(tmp);
		}

		charArrayReader.close();
		bufWriter.flush();
		bufWriter.close();
		return true;
	}

	/**
	 * ByteArrayInputStream 和 ByteArrayOutputStream
	 * 
	 * @return 流的来源或目的地并不一定是文件,也可以是内存中的一块空间,例如一个字节数组
	 *         java.io.ByteArrayInputStream将一个字节数组当作流输入的来源,
	 *         而java.io.ByteArrayOutputStream则可以将一个字节数组当作流输出目的地
	 * @throws IOException
	 */
	public static boolean test_byte_array_stream() throws IOException {
		String str = "qinglonghai";
		byte[] src = str.getBytes();
		ByteArrayInputStream bais = new ByteArrayInputStream(src);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		transform(bais, baos);
		byte[] result = baos.toByteArray();
		System.out.println(new String(result));

		return true;
	}

	public static void transform(InputStream ips, OutputStream ops)
			throws IOException {
		int ch = 0;
		while ((ch = ips.read()) != -1) {
			int upperCh = Character.toUpperCase((char) ch);
			ops.write(upperCh);
		}
	}

	/**
	 * BufferedReader 和 BufferedWriter的使用
	 * 
	 * @param rfileName
	 * @param wfileName
	 * @return
	 * @throws IOException
	 *             FileReader类和FileWriter类:
	 * 
	 *             FileReader类使用字符方法创建文件输入流;FileWriter类是实现以字符方式创建一个输出文件流。
	 * 
	 *             BufferedReader类和BufferedWriter类:
	 * 
	 *             BufferedReader类和BufferedWriter类都是缓冲区,它们是以字符方式缓存流的,
	 *             因此与FileReader类和FileWriter类配合使用。
	 */
	public static boolean test_buffer_read(String rfileName, String wfileName)
			throws IOException {
		BufferedReader bf = new BufferedReader(new FileReader(rfileName));
		BufferedWriter bw = new BufferedWriter(new FileWriter(wfileName, true));// 此处的true表示追加

		String str = null;
		while ((str = bf.readLine()) != null) {
			bw.newLine();// 换行
			bw.write(str);
			bw.flush();
		}

		return true;
	}

	/**
	 * 通过InputStream读取文件内容 通过OutputStream将内容写入文件
	 * 
	 * @param rfileName
	 *            读取文件路径
	 * @param wfileName
	 *            写入路径
	 * @return
	 * @throws IOException
	 * 
	 *             FileInputStream类和FileOutputStream类:
	 *             
	 *             FileInputStream类是文件输入流,它用于接收文件的读取操作;FileOutputStream类是文件的输出流,它用于输出到文件
	 *             。它们都是以字节方式来完成文件的操作。
	 * 
	 *             BufferedInputStream类和BufferedOutputStream类:
	 * 
	 *             BufferedInputStream类是输入缓存;BufferedOutputStream是输出缓存。
	 *             它们通常和FileInputStream类和FileOutputStream类配合使用。也就是说它们也是字节操作的缓存器。
	 */
	public static boolean test_buffered_stream(String rfileName,
			String wfileName) throws IOException {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		bis = new BufferedInputStream(new FileInputStream(rfileName));
		bos = new BufferedOutputStream(new FileOutputStream(wfileName));

		byte[] bb = new byte[10];

		while (bis.read(bb) != -1) {
			bos.write(bb);
			bos.write(2);// 写入一个特别的字符
			bos.flush();
		}
		if (bos != null) {
			bos.close();
		}

		return true;
	}

	/**
	 * 利用集合存储数据需 注意
	 * 
	 * @param rfileName
	 * @param wfileName
	 * @return
	 */
	public static boolean test_buffered_stream_array(String rfileName,
			String wfileName) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(rfileName));
			bos = new BufferedOutputStream(new FileOutputStream(wfileName));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		List<byte[]> slist = new ArrayList<byte[]>();
		byte[] bb = new byte[10];

		try {
			/**
			 * ArrayList里保存的是一组引用,代码里每次不把数组加到ArrayList里去, 数组本身的内容没有被复制,只是引用被复制了
			 * 
			 * 每次写入的都是最后bb里的值
			 */
			while (bis.read(bb) != -1) {
				slist.add(bb);
			}
			/**
			 * 每次读取后将bb的引用指向新new 的一个就可以了
			 */
			while (bis.read(bb) != -1) {
				slist.add(bb);
				bb = new byte[10];
			}
			for (byte[] b : slist) {
				bos.write(b);
				bos.flush();
			}

			if (bos != null) {
				bos.close();
			}
		} catch (IOException e) {
			System.out.println(e.getMessage());
			return false;
		}
		return true;
	}

	/**
	 * @param sourceFile
	 * @param targetFile
	 * @throws IOException
	 * 文件目录的复制
	 */
	private static void copyFile(String sourceName, String targetName) throws IOException {
		File sourceFile=new File(sourceName);
		File targetFile=new File(targetName);
		
		File tarpath = new File(targetFile, sourceFile.getName());
		if (sourceFile.isDirectory()) {
			tarpath.mkdir();
			File[] dir = sourceFile.listFiles();
			for (int i = 0; i < dir.length; i++) {
				copyFile(dir[i].getPath(), tarpath.getPath());
			}
		} else {
			InputStream is = new FileInputStream(sourceFile);
			OutputStream os = new FileOutputStream(tarpath);

			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = is.read(buf)) != -1) {
				os.write(buf);
			}
			System.out.println("复制完成");
			is.close();
			os.close();
		}
	}

	/**
	 * 根据类型写入取出
	 * 
	 * @throws IOException
	 * 
	 */
	public static void test_data_stream() throws IOException {
		InputOrOutput mains = new InputOrOutput();
		Member[] members = { mains.new Member(1, 90, "Just青", '0'),
				mains.new Member(2, 95, "momor龙", '1'),
				mains.new Member(3, 88, "Bush海", '1') };

		DataOutputStream dataOutputStream = new DataOutputStream(
				new FileOutputStream("F:\\testdata\\22.txt"));

		for (Member member : members) {
			// 写入UTF字符串
			dataOutputStream.writeUTF(member.getName());
			// 写入int数据
			dataOutputStream.writeInt(member.getAge());
			dataOutputStream.writeInt(member.getId());
			dataOutputStream.writeChar(member.getSex());
		}

		// 所有数据至目的地
		dataOutputStream.flush();
		// 关闭流
		dataOutputStream.close();

		DataInputStream dataInputStream = new DataInputStream(
				new FileInputStream("F:\\testdata\\22.txt"));

		// 读出数据并还原为对象
		for (int i = 0; i < members.length; i++) {
			// 读出UTF字符串
			String name = dataInputStream.readUTF();
			// 读出int数据
			int score = dataInputStream.readInt();
			int id = dataInputStream.readInt();
			char sex = dataInputStream.readChar();
			members[i] = mains.new Member(score, id, name, sex);
		}

		// 关闭流
		dataInputStream.close();

		// 显示还原后的数据
		for (Member member : members) {
			System.out.println(member.getName() + " " + member.getAge() + " "
					+ member.getId() + " " + member.getSex());
		}

	}

	class Member {
		private String name;
		private int age;
		private char sex;
		private int id;

		public Member() {
			super();
		}

		public Member(int age, int id, String name, char sex) {
			super();
			this.age = age;
			this.id = id;
			this.name = name;
			this.sex = sex;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}

		public char getSex() {
			return sex;
		}

		public void setSex(char sex) {
			this.sex = sex;
		}

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}
	}
}

你可能感兴趣的:(java,F#,OS)