一个长点的字符串,把他转换为字节数组后,想10个字节10个字节的写入文件?
怎么实现10个字节10个字节的写入文件呢?

解决方案 »

  1.   


    package smstest;import java.io.FileOutputStream;
    import java.io.PrintStream;
    import java.lang.reflect.Array;
    import java.util.Arrays;public class fmt {
        public static void main(String args[]) {
            FileOutputStream out; // declare a file output object
            PrintStream p; // declare a print stream object        try {
                // connected to "myfile.txt"
                out = new FileOutputStream("c:/temp/myfile.txt");
                // Connect print stream to the output stream
                p = new PrintStream(out);
                String orstr = "123456789012345678901234567890123456789012345678901234567890123456789012345678901";
                byte[] orstrarr = orstr.getBytes();
                for (int i = 0; i < orstrarr.length;) {
                    byte[] warr = new byte[10];
                    int length = (orstrarr.length - i)/10 > 0 ? 10 : (orstrarr.length - i)%10;
                    System.arraycopy(orstrarr, i, warr, 0, length);
                    p.print(new String(warr));                
                    i = i + length;                
                }
                
                p.close();
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("Error writing to file");
            }
        }}
      

  2.   


    //package com.ricky.www;/*
    一个长点的字符串,把他转换为字节数组后,想10个字节10个字节的写入文件?
    怎么实现10个字节10个字节的写入文件呢? 
    */import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;public class Test{
    public static void main(String[] args)throws  IOException{
    String content = "hello I'm ricky. I love jennie!";
    File file = new File("ricky.bin");
    strToFile(content,file);
    } public static void strToFile(String content,File file)throws IOException{
    if(content == null || content.length() == 0){
    throw new IllegalArgumentException("content = null!");
    }

    byte[] bs = content.getBytes();
    FileOutputStream fos = new FileOutputStream(file);
    FileChannel channel = fos.getChannel();
    final int COUNT = 10;
    ByteBuffer buffer = ByteBuffer.allocate(COUNT);
    int offset = 0; int time = bs.length / COUNT; int length = 0; for(int i = 0 ; i <= time ; i ++){
    offset = COUNT * i;
    length = (COUNT < (bs.length - offset)) ? COUNT : bs.length - offset;
    buffer.put(bs , offset , length);
    buffer.flip();
    System.out.println((i + 1) + ": " + "offset = " + offset + " length: " + length);
    channel.write(buffer);
    buffer.clear();
    } }
    }
      

  3.   


    //package com.ricky.www;/*
        一个长点的字符串,把他转换为字节数组后,想10个字节10个字节的写入文件?
        怎么实现10个字节10个字节的写入文件呢? 
    */
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    import java.io.IOException;
    import java.io.File;
    import java.io.FileOutputStream;
    public class Test2{
    public static void main(String[] args)throws IOException{
    String content = "I'm ricky . I love jennie.";
    File file = new File("ricky.bin");
    strToFile(content,file);
    } public static void strToFile(String content , File file)throws IOException{
    if(content == null || content.length() == 0){
    throw new IllegalArgumentException("content is invalid.");
    } ByteBuffer buffer = ByteBuffer.wrap(content.getBytes()); FileOutputStream fos = new FileOutputStream(file);
    FileChannel channel = fos.getChannel();

    final int COUNT = 10; //设置每次最多传输的字节数. int position = 0;
    int temp = buffer.remaining(); int limit = COUNT < temp ? COUNT : temp; //设置第一次传输的长度. do {
    temp = buffer.capacity() - limit;
    buffer.limit(limit).position(position); position = limit;
    limit += COUNT < temp ? COUNT : temp; channel.write(buffer); if(temp == 0){
    break;
    }//这里当limit和buffer.capacity()相等的时候需要写入后退出循环

    }while(true); channel.close();
    fos.close();
    }
    }
    用这个更加有效率些.