package com.huawei.wang; import java.io.*; /** * io流操作 * 如果需要在文件后面追加 * 用FileWriter,BufferWriter,RamdomAccessFile操作文件 */ public class fileTest { public static void main(String[] args) { try{ test5(); }catch (Exception e){ e.printStackTrace(); } } private static void test() throws IOException { File file = new File("C:\\Users\\wwx562990\\Desktop\\create or replace procedure P_C_SSOC_FINDUNITATTR"); try { FileInputStream fileInputStream = new FileInputStream(file); //FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\wwx562990\\test.txt"); int size = fileInputStream.available(); byte[] bytes = new byte[size]; fileInputStream.read(bytes); System.out.println(new String(bytes)); /*int i; while(( i=fileInputStream.read(bytes))!=-1){ fileOutputStream.write(bytes); fileOutputStream.flush(); }*/ System.out.println(); } catch (FileNotFoundException e) { e.printStackTrace(); } } private static void test2() throws IOException { File file = new File("C:\\Users\\wwx562990\\Desktop\\create or replace procedure P_C_SSOC_FINDUNITATTR"); try { FileInputStream fileInputStream = new FileInputStream(file); FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\wwx562990\\Desktop\\test12.sql"); byte[] bytes = new byte[1024]; int i; while(( i=fileInputStream.read(bytes))!=-1){ fileOutputStream.write(bytes,0,i); } System.out.println(new String(bytes)); } catch (FileNotFoundException e) { e.printStackTrace(); } } private static void test3() throws IOException { File file = new File("C:\\Users\\wwx562990\\Desktop\\123","svgdfgdf"); File file1 = new File( file , "qw.txt" ) ; if(!file.exists()){ file.mkdirs(); } file1.createNewFile(); System.out.println(file1.getPath()); } private static void test4() throws IOException { File File = new File("C:\\Users\\wwx562990\\Desktop\\123\\svgdfgdf"); File oldFile = new File(File,"qw.txt"); File newFile = new File(File,"fasf.sql"); InputStream inputStream = null; BufferedInputStream bufferedInputStream = null; OutputStream outputStream = null; BufferedOutputStream bufferedOutputStream = null; inputStream = new FileInputStream(oldFile); bufferedInputStream = new BufferedInputStream(inputStream); outputStream = new FileOutputStream(newFile); bufferedOutputStream = new BufferedOutputStream(outputStream); byte[] b = new byte[1024]; //代表一次最多读取1KB的内容 int length = 0; //代表实际读取的字节数 while ((length = bufferedInputStream.read(b)) != -1) { //length 代表实际读取的字节数 bufferedOutputStream.write(b,0,length); } //缓冲区的内容写入到文件 bufferedOutputStream.flush(); } private static void test5() throws Exception{ String mes = "你好,world" ; byte[] b = mes.getBytes("UTF-8"); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream() ; try { byteArrayOutputStream.write( b ); FileOutputStream fileOutputStream = new FileOutputStream( new File( "C:\\Users\\wwx562990\\Desktop\\123\\svgdfgdf\\qw.txt" ) ) ; byteArrayOutputStream.writeTo( fileOutputStream ) ; fileOutputStream.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally{ try { byteArrayOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
io文件基本操作
最新推荐文章于 2022-10-25 22:05:40 发布