IO, NIO to read and write files

#java NIO获取文件

public String getCaseFileInfo(String fileName) {
        StringBuffer sb = new StringBuffer();
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(new File(fileName));
            FileChannel fileChannel = inputStream.getChannel();
            Charset charset = Charset.forName("utf-8");
            CharsetDecoder charsetDecoder = charset.newDecoder();
            int capacity = 1024;
            ByteBuffer buffer = ByteBuffer.allocate(capacity);
            CharBuffer charBuffer = CharBuffer.allocate(capacity);
            System.out.println("buffer limit and capacity : = [" + buffer.limit() + buffer.capacity() + "]");

            int length = -1;
            try {
                while ((length = fileChannel.read(buffer)) != -1) {
                    buffer.clear();
                    charsetDecoder.decode(buffer, charBuffer, true);
                    //                    byte[] bytes = buffer.array();
                    String s = new String(charBuffer.toString());
                    sb.append(s);
                    System.out.println("s = [" + s + "]");
                }
                fileChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
#java IO读取文件

 //read content file using java IO stream

    public static String readFile(String filePathAndName) {
        String fileContent = "";
        try {
            File f = new File(filePathAndName);
            if (f.isFile() && f.exists()) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(f), "UTF-8");
                BufferedReader reader = new BufferedReader(read);
                String line;
                while ((line = reader.readLine()) != null) {
                    fileContent += line;
                }
                read.close();
            }
        } catch (Exception e) {
            System.out.println("读取文件内容操作出错");
            e.printStackTrace();
        }
        return fileContent;
    }

#java IO write 文件

    //write content file using java IO stream

    public static void writeFile(String filePathAndName, String fileContent) {
        try {
            File f = new File(filePathAndName);
            if (!f.exists()) {
                f.createNewFile();
            }
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(fileContent + "\r\n");
            writer.close();
        } catch (Exception e) {
            System.out.println("写文件内容操作出错");
            e.printStackTrace();
        }
    }

#java nio write文件

 public void writeFileState(String fileName, String text) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(new File(fileName));
            FileChannel fileChannel = fileOutputStream.getChannel();
            ByteBuffer byteBuffer = Charset.forName("GBK").encode(text);
            int length = 0;
            try {
                while ((length = fileChannel.write(byteBuffer)) != 0) {
                    System.out.println("file length have written: " + length);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

Guess you like

Origin www.cnblogs.com/zhidaCoding/p/11289647.html