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包中. 常用方法如下 ...
随机推荐
- Net Core 的公共组件之 Http 请求客户端
Net Core 的公共组件之 Http 请求客户端 想必大家在项目开发的时候应该都在程序中调用过自己内部的接口或者使用过第三方提供的接口,咱今天不讨论 REST ,最常用的请求应该就是 GET 和 ...
- VLC编译问题
在Ubuntu下编译VLC源代码生成的VLC无法播放Youtube视频(比如https://www.youtube.com/watch?v=mDp-ABzpRX8) 错误提示如下: zlf@ubunt ...
- 用PHP对数据库数据进行删除
显示页面: <body> <table width="100%" border="1" cellpadding="0" c ...
- 1566: [NOI2009]管道取珠 - BZOJ
Description Input第一行包含两个整数n, m,分别表示上下两个管道中球的数目. 第二行为一个AB字符串,长度为n,表示上管道中从左到右球的类型.其中A表示浅色球,B表示深色球. 第三行 ...
- 二分图匹配(KM算法)n^4 分类: ACM TYPE 2014-10-04 11:36 88人阅读 评论(0) 收藏
#include <iostream> #include<cstring> #include<cstdio> #include<cmath> #incl ...
- Binary Indexed Tree 分类: ACM TYPE 2014-08-29 13:08 99人阅读 评论(0) 收藏
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n, ...
- angularJs 问题
1. IE不能渲染指令中的 style="background-color",而chrome和firefox可以 <!DOCTYPE html> <html ng ...
- Load hlsl
这个函数和sample差不多 不过没有samplestate和filter http://msdn.microsoft.com/zh-cn/library/windows/desktop/bb5096 ...
- [百度空间] [原]CImageList支持32位透明位图
32位的位图主要是包含Alpha值(0-255)可以有半透效果的.之前用FreeImage加载 的DIB, CImageList直接绘制会有黑色背景.即便用了ILC_MASK,也创建了mask map ...
- bootstrap学习记录(慕课网教程)
1.当主标题下需要副标题时可在h中嵌套small<h1>nihao<small>a</samll></h1> 2.当某一段落需要突出显示时可添加lead ...