Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)
import java.io.*;
class ByteArrayStreamDemo
{
public static void main(String[] args)
{
//数据源
ByteArrayInputStream bis = new ByteArrayInputStream("ABCDEF".getBytes());//getBytes()是将一个字符串转化为一个字节数组 //数据目的
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int by = 0;
while((by = bis.read())!=-1)
{
bos.write(by);
} System.out.println(bos.size());
System.out.println(bos.toString()); //bos.writeTo(new FileOutputStream("ByteArray.txt"));//只有此处需要对异常进行处理。
}
}
import java.io.*;
class CharArrayStreamDemo
{
public static void main(String[] args)throws IOException
{
//数据源
CharArrayReader car = new CharArrayReader("世界第一等".toCharArray());//toCharArray()是将一个字符串转化为一个字符数组 //数据目的
CharArrayWriter caw = new CharArrayWriter();
int by = 0;
while((by = car.read())!=-1)
{
caw.write(by);
} System.out.println(caw.size());
System.out.println(caw.toString()); //caw.writeTo(new FileOutputStream("CharArray.txt"));//需要对异常进行处理。
}
}
import java.io.*;
class DataStreamDemo
{
public static void main(String[] args)throws IOException
{
//WriteData();
//ReadData();
WriteUTFDemo();
ReadUTFDemo();
}
public static void WriteData()throws IOException
{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("Data.txt"));
dos.writeInt(10);
dos.writeChar(97);
dos.writeDouble(3.1415926);
dos.writeBoolean(true);
dos.close();
}
public static void ReadData()throws IOException
{
DataInputStream dis = new DataInputStream(new FileInputStream("Data.txt"));
System.out.println(dis.readInt());
System.out.println(dis.readChar());
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
dis.close();
} public static void WriteUTFDemo()throws IOException
{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("Data-UTF.txt"));
dos.writeUTF("你好");
dos.close();
}
public static void ReadUTFDemo()throws IOException
{
DataInputStream dis = new DataInputStream(new FileInputStream("Data-UTF.txt"));
System.out.println(dis.readUTF());
dis.close();
}
}
import java.io.*;
class ReadPiped implements Runnable
{
private PipedInputStream in;
ReadPiped(PipedInputStream in)
{
this.in = in;
}
public void run()
{
try
{
byte[] buf = new byte[1024]; System.out.println("读取前,没有数据,管道阻塞开始");
int len = in.read(buf);
System.out.println("读取数据,管道阻塞结束"); String s = new String(buf,0,len);
System.out.println(s);
}
catch(IOException e)
{
throw new RuntimeException("管道读取流失败!");
}
finally
{
try
{
in.close();
}
catch(IOException e)
{
throw new RuntimeException("流关闭失败!");
}
}
}
}
class WritePiped implements Runnable
{
private PipedOutputStream out;
WritePiped(PipedOutputStream out)
{
this.out = out;
}
public void run()
{
try
{
System.out.println("开始写入数据,等待5秒");
Thread.sleep(5000);
out.write("guandao lai le".getBytes());
out.close();
}
catch(Exception e)
{
throw new RuntimeException("管道输出流失败!");
}
}
}
class PipedStreamDemo
{
public static void main(String[] args)throws IOException
{
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);//使此管道输入流连接到管道输出流 ReadPiped r = new ReadPiped(in);
WritePiped w = new WritePiped(out); new Thread(r).start();
new Thread(w).start();
}
}
//例子5:
import java.io.*;
class PrintStreamDemo
{
public static void main(String[] args)throws IOException
{
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); PrintWriter ps = new PrintWriter(System.out,true);//(new BufferedWriter(new FileWriter("F:\\myfile\\print.txt")),true) String line = null; while((line = bufr.readLine())!=null)
{
if("over".equals(line))
break;
ps.println(line.toUpperCase());
//ps.flush();
}
ps.close();
bufr.close();
}
}
import java.io.*;
import java.util.*;
class PropertiesDemo
{
public static void main(String[] args)throws IOException
{
//setAndget();
//method_1();
method_2();
}
//演示如何将流中的数据添加放到集合中
//想要将key-value.txt数据存到集合进行操作
/*
步骤:1.用一个流和key-value相关联
2.读一行数据,将该行数据用"="进行切割
3.等号左边作为键,右边作为值,存入到Properties集合即可。
*/
//方法1(load方法的原理)
public static void method_1()throws IOException
{
Properties p = new Properties();
//FileInputStream fis = new FileInputStream("F:\\myfile\\key-value.txt");
BufferedReader bufr = new BufferedReader(new FileReader("F:\\myfile\\key-value.txt"));
String str=null;
while((str=bufr.readLine())!=null)
{
String[] s = str.split("=");
System.out.println(s[0]+"...."+s[1]);
p.setProperty(s[0],s[1]);
}
//System.out.println(p);
p.list(System.out);
bufr.close();
}
//方法2(直接调用load方法)
public static void method_2()throws IOException
{
Properties p = new Properties();
p.load(new FileReader("F:\\myfile\\key-value.txt"));//p.load(new FileInputStream("F:\\myfile\\key-value.txt")) p.setProperty("zhaoliu","100");//修改已有的姓名的年龄并保存在文件中
p.store(new FileWriter("F:\\myfile\\key-value.txt"), p.toString());
System.out.println(p);
} //设置和获取元素
public static void setAndget()
{
Properties p = new Properties();
p.setProperty("zhangsan","30");
p.setProperty("lisi","40"); System.out.println(p); String value = p.getProperty("lisi");
System.out.println("value="+value); //p.setProperty("lisi",89+"");//修改lisi年龄 Set<String> name = p.stringPropertyNames();
Iterator<String> it = name.iterator();
while(it.hasNext())
{
String key = it.next();
System.out.println("key:"+key+" "+"value"+p.getProperty(key));
}
/*
for(String key: name)
{
System.out.println("key:"+key+" "+"value"+p.getProperty(key));
}
*/
}
}
import java.io.*;
class RandomAccessFileDemo
{
public static void main(String[] args)throws IOException
{
//WriteFile();
//WriteFile2();
ReadFile();
}
public static void WriteFile()throws IOException
{
RandomAccessFile raf = new RandomAccessFile("Random.txt","rw");
raf.write("李四".getBytes());
raf.writeInt(65);
raf.write("王武".getBytes());
raf.writeInt(67);
raf.close();
} public static void WriteFile2()throws IOException
{
//改变指针,随机位置写入数据,还可以修改之前位置所写的数据(不会重新覆盖创建的文件)
RandomAccessFile raf = new RandomAccessFile("Random.txt","rw");
raf.seek(8*3);
raf.write("周七".getBytes());
raf.writeInt(69);
raf.seek(8*2);
raf.write("赵六".getBytes());
raf.writeInt(68);
raf.close();
} public static void ReadFile()throws IOException
{
RandomAccessFile raf = new RandomAccessFile("Random.txt","r");
PrintStream ps = System.out;
System.setOut(ps);
byte[] buf = new byte[4];
//ps.println(raf.getFilePointer());//获取当前指针位置
//raf.seek(8*1);//改变当前指针的位置(可以前后移动)
//raf.skipBytes(8);//跳过8个字节(只能往后跳)
while(raf.read(buf)!=-1)
{
String name = new String(buf);
ps.println("name="+name); int age= raf.readInt();
ps.println("age="+age); }
raf.close();
}
}
import java.io.*;
import java.util.*;
class SequenceStreamDemo
{
public static void main(String[] args)throws IOException
{
Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(new FileInputStream("F:\\myfile\\1.txt"));
v.add(new FileInputStream("F:\\myfile\\2.txt"));
v.add(new FileInputStream("F:\\myfile\\3.txt")); Enumeration<FileInputStream> enu = v.elements();
SequenceInputStream sis = new SequenceInputStream(enu);//将多个流整合为一个流对象 BufferedWriter bufw = new BufferedWriter(new FileWriter("F:\\myfile\\123.txt")); int num = 0;
while((num=sis.read())!=-1)
{
bufw.write(num);
bufw.flush();
}
sis.close();
bufw.close();
}
}
import java.io.*;
class StringReaderWriterDemo
{
public static void main(String[] args)throws IOException
{
StringReader sr = new StringReader("Thank you very much");
StringWriter sw = new StringWriter(); //StringWriter s = sw.append('A');
int by = 0;
while((by=sr.read())!=-1)
{
sw.write(by);
} System.out.println(sw.toString());
}
}
import java.io.*;
class ObjectStreamDemo
{
public static void main(String[] args)throws Exception
{
//writeObj();
readObj();
}
public static void writeObj()throws IOException
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Object.txt"));
oos.writeObject(new Person("zhangsan",25));
oos.close();
}
public static void readObj()throws Exception
{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Object.txt"));
Person p =(Person)ois.readObject();
System.out.println(p);
ois.close();
}
}
Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)的更多相关文章
- java.io中流的操作:字节流、字符流
java.io中流的操作:字节流.字符流(1)使用File类打开一个文件(2)通过字节流或字符流的子类指定输出的位置(3)进行读/写操作(4)关闭输入/输出 1.字节流:主要是byte类型数据,以by ...
- java io包File类
1.java io包File类, Java.io.File(File用于管理文件或目录: 所属套件:java.io)1)File对象,你只需在代码层次创建File对象,而不必关心计算机上真正是否存在对 ...
- String.getBytes()和String.tocharArray(),字节数组和字符数组的区别
String.getBytes()是将字符串转化为一个字节数组.而String.toCharArray()是将一个字符串转化为一个字符数组. [例如] byte bys[] ="国庆60周年 ...
- JAVA IO分析二:字节数组流、基本数据&对象类型的数据流、打印流
上一节,我们分析了常见的节点流(FileInputStream/FileOutputStream FileReader/FileWrite)和常见的处理流(BufferedInputStream/B ...
- java IO之 File类+字节流 (输入输出 缓冲流 异常处理)
1. File类
- [转]探究java IO之FileInputStream类
使用FileInputStream类创建的InputStream对象可以用于从文件读取内容.两个常用的构造函数如下所示: ? 1 2 FileInputStream(String filePath) ...
- Java IO(文件操作工具类)
FileOperate实现的功能: 1. 返回文件夹中所有文件列表 2. 读取文本文件内容 3. 新建目录 4. 新建多级目录 5. 新建文件 6. 有编码方式的创建文件 7. 删除文件 8. 删除指 ...
- Java IO 之 System类
1.使用System.in.read读取,使用System.out.println 输出 package org.zln.io; import java.io.IOException; /** * C ...
- JAVA IO:Scanner类
使用Scanner类接收输入数据. JAVA提供了专门的输入数据类,此类可以完成BufferedReader类的功能,也可以方便的对输入数据进行验证,此类存放于JAVA.UTILL包中. 常用方法如下 ...
随机推荐
- 四则运算2+psp0级表格
四则运算2 一.题目和要求 题目:写一个能自动生成小学四则运算题目的程序,要求一次输出不少于30道,只能是整数100以内的四则运算(四则运算1升级版) 要求: 1.题目避免重复 2.可定制(数量/打印 ...
- c语言编程之二叉排序树
二叉排序树,又称为二叉查找树.它是一颗空树,或者是具有下面的性质的二叉树: 1.若它的左子树不空,则左子树上所有节点的值均小于它的根结构的值: 2.若它的右子树不空,则右子树上所有节点的值均大于它的根 ...
- JAVA开发CHECK STYLE
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE module PUBLIC "-/ ...
- 三门概率问题之C#版
前言: 早上看到一片关于三门问题的博客http://www.cnblogs.com/twocats/p/3440398.html,抱着该博客结论的怀疑态度用C#语言写了一些代码.实验证明该博客的结论是 ...
- python-面向对象(股票对象举例)
股票对象实例 class Stock(object): def __init__(self,stockCode ,stockName,averagePrice_yesterday,averagePri ...
- poj 2342 Anniversary party
题目链接:http://poj.org/problem?id=2342 题意:读题很容易懂,这里不做介绍. 解法:树形DP之路的第一道题. #include<iostream> #incl ...
- VS Bug 当获取其他项目的代码时, F5 无法进入调试模式. 也不报错....
在64位的机子下, 被获用的项目使用X86时会出现. 就会出现 F5 无法进入调试模式. 也不报错.... 打断点也没有用. 在不加入X86项目的代码时, 又可以运行.. 解决方案: 检查 ...
- C语言预处理命令
1.#error Directive (C/C++) The #error directive emits a user-specified error message at compile time ...
- ios7.1 在线安装 失败的解决办法
昨天升级7.1之后,证书制作的app就无法在线安装了,各种搜..说需要https.搭web服务器,起https服务,结果不行.终于明白,https需要权威认证,自己搭建的不行,需要买第三方权威机构的认 ...
- CSS预处理器实践之Sass、Less大比拼[转]
什么是CSS预处理器? CSS可以让你做很多事情,但它毕竟是给浏览器认的东西,对开发者来说,Css缺乏很多特性,例如变量.常量以及一些编程语法,代码难易组织和维护.这时Css预处理器就应运而生了.Cs ...