private static String path="D:\\work_idea\\Idea_workspace\\FileTextWorkSpace\\HelloFile.txt"; public static void main(String[] args) { // InputFIle(); // OutputFile(); // BufferInputFile(); // BufferOutputFile(); byteFlowToStringFlowAboutInput(); // byteFlowToStringFlowAboutOutput(); } //字节流读取(input) /* * 1:创建一个读取文件的File对象 * 2:对File对象创建一个读入流 * 3:创建一个byte类型的数组存储文本内的内容,长度为文本的长度大小 * 4:读取文件内的信息 * */ static void InputFIle(){ File file = new File("D:\\work_idea\\Idea_workspace\\FileTextWorkSpace\\HelloFile.txt"); try { InputStream input=new FileInputStream(file); //创建byte类型的数组 byte[] b =new byte[(int)file.length()]; //读取文本内容信息并存入数组中 input.read(b); input.close(); //将其转换成字符串 System.out.println(new String(b)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //字节流写入(outPut) static void OutputFile(){ /* * 1:创建一个File对象 * 2;为这个File对象创建一个输出流 * 3;从键盘输入需要向File对象输入的信息 * 4:创建byte【】数组将输入的信息转化为byte类型 * 5:输入信息 * */ File file = new File("D:\\work_idea\\Idea_workspace\\FileTextWorkSpace\\HelloFile.txt"); //创建输入流 try { //定义append 方法为ture ,允许追加,而不是覆盖 OutputStream output= new FileOutputStream(file,true); //从键盘输入 //创建Scanner对象 Scanner scanner=new Scanner(System.in); //获取键盘输入的信息 System.out.println("请输入一个任意长度的字符串"); String str=scanner.nextLine(); //创建byte类型的数组,并将String类型的数据转化为字节类型 byte [] bytes=str.getBytes(); //输出 try { output.write(bytes); output.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } //缓存区的应用 static void BufferInputFile(){ /* * 1:创建File对象 * 2:创建input输入流File对象 * 3:定义缓存区 * 4:将读取的信息存储在缓存区中 * 5:从缓存区读取数据 * */ File file =new File("D:\\work_idea\\Idea_workspace\\FileTextWorkSpace\\HelloFile.txt"); //定义输入流 try { InputStream stream=new FileInputStream(file); BufferedInputStream bufferedInputStream=new BufferedInputStream(stream); byte[] bytes=new byte[(int)file.length()]; bufferedInputStream.read(bytes); System.out.println(Arrays.toString(bytes)); System.out.println(new String(bytes)); bufferedInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } static void BufferOutputFile(){ /* * 1:定义一个File类的对象 * 2:为File对象创建一个输出流 * 3:为输出流创建一个缓存流 * 4;定义一个Scanner对象,从键盘获取信息 * 5:String类型的字符串转化为byte类型 * 6:将获取到的信息写入到缓存流中 * 7;输出 * */ //1 File file=new File("D:\\work_idea\\Idea_workspace\\FileTextWorkSpace\\HelloFile.txt"); //2 try { OutputStream outputStream=new FileOutputStream(file,true); //3 BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(outputStream); //4 Scanner scanner=new Scanner(System.in); System.out.println("请输入一个字符串"); String str=scanner.nextLine(); byte[] bytes=str.getBytes(); bufferedOutputStream.write(bytes); bufferedOutputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //字节流转化为字符流 即转换流 static void byteFlowToStringFlowAboutInput(){ //创建File对象 File file=new File(path); //为File对象创建一个输入流 try { InputStream inputStream=new FileInputStream(file); InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"utf-8"); //定义变量保存字符 int reads; while ((reads=inputStreamReader.read())!=-1){ System.out.print((char) reads); } inputStreamReader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } static void byteFlowToStringFlowAboutOutput() { //定义FIle File file=new File(path); //输出流 try { OutputStream outputStream=new FileOutputStream(file,true); OutputStreamWriter outputStreamWriter=new OutputStreamWriter(outputStream,"utf-8"); //定义scanner Scanner scanner=new Scanner(System.in); System.out.println("请输入一个字符串"); String str= scanner.nextLine(); //输出 outputStreamWriter.write(str); outputStreamWriter.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
字节流,字节缓冲流,字节流转化为字符流的输入与输出
最新推荐文章于 2023-12-20 21:21:58 发布