一个长点的字符串,把他转换为字节数组后,想10个字节10个字节的写入文件?
怎么实现10个字节10个字节的写入文件呢?
怎么实现10个字节10个字节的写入文件呢?
解决方案 »
- 关于java读取PDF文件
- 如何解决防盗链的问题?
- B/S中JAVA是否可以获得当前域用户的用户名
- 求Java写的对一个字符串进行加密,和对这加密后的字符解密,返回一个字符串的程序
- 名词解释----对象引用
- java j2se基础类进不去了,只能在这个栏里问了~(关于连接数据库,并查询的问题)
- swing中如何获取本机电脑名称?
- 我想问一下,javax和java包是放在哪个.jar文件中
- Java的包如何掌握?
- 文件存储问题!帮帮忙 很急!
- 求异常解决方法:Exception in thread "main" java.lang.NoClassDefFoundError:
- 用netBeans开发的Java swing程序上不显示进度条
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");
}
}}
//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();
} }
}
//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();
}
}
用这个更加有效率些.