18、IO流-标准流

1、标准输入输出流
   System.in
   System.out
  2、转换流
   InputStreamReader
   OutputStreamWriter
  3、字节缓冲流
  4、字符缓冲流

/**
	 * System.in 方法1
	 */
	public static void startInput(){
		String result = null;
		BufferedReader bufferedReader = null;
		try {
			System.out.println("请输入字符 ");
			bufferedReader = new BufferedReader(new InputStreamReader(System.in));
			result = bufferedReader.readLine();
			System.out.println(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * System.in 用法2 
	 */
	public static void startInput2(){
		byte []b = new byte[1024];
		int count = 0;
		try {
			System.out.println("请输入字符 ");
			count = System.in.read(b);
			System.out.println(count);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	/**
	 * System.out 用法1 
	 */
	public static void startOutput(){
		try {
			PrintStream printStream= new PrintStream("C:\\Users\\Administrator\\Desktop\\AAA.tet");
			System.setOut(printStream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * System.out 用法2 
	 */
	public static void startOutput2(){
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
		PrintStream printStream = new PrintStream(byteArrayOutputStream);
		
		
		PrintStream systemPrintStream = System.out;//保存System.out
		System.setOut(printStream);
		String a = System.out.toString();
		String b = printStream.toString();
		String c = systemPrintStream.toString();
		
		System.out.println("printStream  " + printStream);
		System.out.println("哈哈哈");
		String infoString = byteArrayOutputStream.toString();
		System.setOut(systemPrintStream);
		System.out.println("infoString " + infoString);
		System.out.println(a + " "+ b + " " + c);
	}
	
	/*
	 * System.out用法3
	 */
	public static void outPrintf2() {
		// 定义一些变量,用来格式化输出。
		double d = 345.678;
		String s = "你好!";
		int i = 1234;
		// "%"表示进行格式化输出,"%"之后的内容为格式的定义。
		System.out.printf("%f", d);// "f"表示格式化输出浮点数。
		System.out.println();
		System.out.printf("%9.2f", d);// "9.2"中的9表示输出的长度,2表示小数点后的位数。
		System.out.println();
		System.out.printf("%+9.2f", d);// "+"表示输出的数带正负号。
		System.out.println();
		System.out.printf("%-9.4f", d);// "-"表示输出的数左对齐(默认为右对齐)。
		System.out.println();
		System.out.printf("%+-9.3f", d);// "+-"表示输出的数带正负号且左对齐。
		System.out.println();
		System.out.printf("%d", i);// "d"表示输出十进制整数。
		System.out.println();
		System.out.printf("%o", i);// "o"表示输出八进制整数。
		System.out.println();
		System.out.printf("%x", i);// "x"表示输出十六进制整数。
		System.out.println();
		System.out.printf("%#x", i);// "x"表示输出带有十六进制标志的整数。
		System.out.println();
		System.out.printf("%s", s);// "s"表示输出字符串。
		System.out.println();
		System.out.printf("输出一个浮点数:%f,一个整数:%d,一个字符串:%s", d, i, s);
		// 可以输出多个变量,注意顺序。
		System.out.println();
		System.out.printf("字符串:%2$s,%1$d的十六进制数:%1$#x", i, s);
		// "X$"表示第几个变量。
	}

附几种IO流文件读写方式(汉字不会读)

public class IOUtils {
	/**
	 *1、 以字符流方式文件,输入流
	 * BufferedReader
	 * FileReader
	 */

	public static void readFileByBuffReadLine(File file) {
		BufferedReader bufferedReader  = null;
		String tempString = "";
		try {
 			bufferedReader = new BufferedReader(new FileReader(file));
			boolean flag = false;
			while((tempString = bufferedReader.readLine()) != null){//读入的数据不为空
				String []tempsStrings = tempString.split(" ");//剪切存储数组
				if (tempsStrings[0].matches("\\d{3}")) {  //判断该字符串是否满足三个数字
					System.out.println(tempsStrings[0]);
					System.out.println(tempsStrings[1]);
					flag = true;
				}else if (flag) {
					System.out.println(tempString.trim());//忽略空格输出
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO: handle exception
		}finally{
			try {
				bufferedReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	/**
	 *1、以字符流读取文件,按字符读取
	 * InputStreamReader
	 * FileInputStream
	 */
	
	public static void readFileByStreamReader(File file){
		Reader reader = null;		
		try {
			reader  = new InputStreamReader(new FileInputStream(file));
			int tempChar;
			String tempString = "";
			boolean flag = false;
			while((tempChar = reader.read()) != -1){//reader.reader()读入数据对应ASCII表 的int值 ,当读入不空
				if ((char)tempChar != '\n') {
					tempString += String.valueOf((char)tempChar);	
				}else{
					String []tempsStrings = tempString.split(" ");//剪切存储数组
					if (tempsStrings[0].matches("\\d{3}")) {  //判断该字符串是否满足三个数字
						flag = true;
						System.out.println(tempsStrings[0]);
						
						if (tempsStrings.length == 3) {
							System.out.println(tempsStrings[1]);
						}else {
							System.out.print(tempsStrings[1]);
						}
							
					}else {
						
						if (flag) {
							System.out.println(tempString.trim());//忽略空格输出
						}
					}
//					System.out.println("读入的数据"+ tempString);
//					System.out.print(tempString);
					tempString = "";
				}
					
//				System.out.print((char)tempChar);
			}
			
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO: handle exception
		}finally{
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	
	/**
	 *3、 以字节为单位读取文件
	 * FileInputStream
	 */
	public static void readByFileInputStream(File file){
		InputStream inputStream = null;
		byte[] tempbyte = new byte[1024];
		int i = 0;
		int byteRead = 0;
		try {
			inputStream = new FileInputStream(file);
			while((byteRead = inputStream.read(tempbyte)) != -1){//read()传参数表示读的是个数,不传读的是对应的ASC码表
				System.out.println("每次读的字节数 "+ byteRead);
				System.out.println(new String(tempbyte, 0, byteRead));	
				
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 *4、 以文件字节流方式写入内容
	 * FileOutputStream
	 */
	public static void writeFileOutPutStream(File file, String content){
		FileOutputStream fileOutputStream = null;
		try {
			
			fileOutputStream = new FileOutputStream(file);
			byte[] tempBytes = content.getBytes();//字符串转字节数组
			fileOutputStream.write(tempBytes);//写入流
			fileOutputStream.flush();//从流写入.txt文件
			
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}finally{
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
				
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 *5、 以字符流的方式写入文件
	 * BufferedWriter
	 * PrintWriter
	 */
	public static void writerByBufferWriter(File file, String contents){
		BufferedWriter bufferedWriter = null;
		try {
			bufferedWriter = new BufferedWriter(new PrintWriter(file));
			bufferedWriter.write(contents);
			bufferedWriter.flush();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (bufferedWriter != null) {
				try {
					bufferedWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 *6、 以字符流的方式写入文件//可追加文件
	 * BuffereWriter
	 * FileWriter
	 */
	public static void writerByBufferWriterAdd(File file, String contents){
		BufferedWriter bufferedWriter = null;
		
		try {
			bufferedWriter = new BufferedWriter(new FileWriter(file, true));
			bufferedWriter.write(contents);
			bufferedWriter.flush();
	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (bufferedWriter != null) {
				try {
					bufferedWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	

}



 

你可能感兴趣的:(18、IO流-标准流)