标签:transient tostring hit new pat 数据操作 std tar runnable
");
}
/*BufferedReader仅仅能接受字符流的缓冲区。由于每个中文须要占领两个字节,所以须要将System.in这个字节输入流变为字符输入流,採用*/
@Test
public void testBuffer(){
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));//将字节流转化为字符流
String str = null;
System.out.println("请输入内容");
try{
str = buf.readLine();
}catch(IOException e){
e.printStackTrace();
}
System.out.println("你输入的内容是:" + str);
}
/*数据操作流DataOutputStream、DataInputStream类*/
@Test
public void testData() throws IOException{
init();
File file=new File(str);
char[] ch = { ‘A‘, ‘B‘, ‘C‘ };
DataOutputStream out=new DataOutputStream(new FileOutputStream(file,true));
for(char temp : ch){
out.writeChar(temp);
}
out.close();
}
/*合并流 SequenceInputStream*/
@Test
public void testSequence() throws IOException{
File file1 = new File("d:" + File.separator +"test"+File.separator+"test.txt");
File file2 = new File("d:" + File.separator +"test"+File.separator+"hello2.txt");
File file3 = new File("d:" + File.separator +"test"+File.separator+"hello.txt");
if(!file1.exists()){
file1.createNewFile();
}
if(!file2.exists()){
file2.createNewFile();
}
if(!file3.exists()){
file3.createNewFile();
}
InputStream input1 = new FileInputStream(file1);
InputStream input2 = new FileInputStream(file2);
OutputStream output = new FileOutputStream(file3);
// 合并流
SequenceInputStream sis = new SequenceInputStream(input1, input2);
int temp = 0;
while((temp = sis.read()) != -1){
output.write(temp);
}
input1.close();
input2.close();
output.close();
sis.close();
}
/*文件压缩 ZipOutputStream类*/
@Test
public void testZip() throws IOException{
init();
File file=new File(str);
String path="d:"+File.separator+"test"+File.separator+"test.zip";
File zipFile=new File(path);
InputStream in=new FileInputStream(file);
ZipOutputStream zip=new ZipOutputStream(new FileOutputStream(zipFile));
zip.putNextEntry(new ZipEntry(file.getName()));
// 设置凝视
zip.setComment("hello");
int temp = 0;
while((temp = in.read()) != -1){
zip.write(temp);
}
in.close();
zip.close();
}
/*解压缩ZipFile类*/
@Test
public void testZipFile() throws ZipException, IOException{
init();
String str="d:"+File.separator+"test"+File.separator+"test.zip";
String path="d:"+File.separator+"test"+File.separator+"zip.txt";
File file=new File(str);
File zipf=new File(path);
ZipFile zipFile = new ZipFile(file);
System.out.println("压缩文件的名称为:" + zipFile.getName());
ZipEntry entry = zipFile.getEntry("test.txt");
InputStream input = zipFile.getInputStream(entry);
OutputStream output = new FileOutputStream(zipf);
int temp = 0;
while((temp = input.read()) != -1){
output.write(temp);
}
input.close();
output.close();
}
/*PushBackInputStream回退流*/
@Test
public void testPush() throws IOException{
String str = "hello,rollenholt";
PushbackInputStream push = null;
ByteArrayInputStream bat = null;
bat = new ByteArrayInputStream(str.getBytes());
push = new PushbackInputStream(bat);
int temp = 0;
while((temp = push.read()) != -1){
if(temp == ‘,‘){
push.unread(temp);
temp = push.read();
System.out.print("(回退" + (char) temp + ") ");
}else{
System.out.print((char) temp);
}
}
}
/*System 类的一些操作*/
@Test
public void testSys(){
System.out.println("系统默认编码为:" + System.getProperty("file.encoding"));
}
/**
* 将一个对象流化即实现Serializable接口(默认将所有属性序列化)还能够实现一组对象的序列化
* 假设不想所有属性被实例化能够在实体中使用如private transient String name; transient属性
* 能够在对象输入(ObjectInputStream)输出(ObjectOutputStream)流中直接操作对象
*
* 实现Externalizable接口。能够将部分属性实例化
*
*
* @throws IOException
* @throws ClassNotFoundException
*/
/*ObjectOutputStream*/
@Test
public void testObject() throws IOException, ClassNotFoundException{
init();
File file = new File(str);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file,true));
oos.writeObject(new Person("laoma", 20,"男"));
oos.close();
//查看
ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
Object obj = input.readObject();
input.close();
System.out.println(obj);
}
/*PushBackInputStream回退流*/
@Test
public void testRect(){
}
}
參考自:http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html
标签:transient tostring hit new pat 数据操作 std tar runnable
原文地址:http://www.cnblogs.com/ljbguanli/p/7293832.html