java,数据流,java数据流

字节流:::

byte[] buffer = new byte[50];
int a = System.in.read(buffer);
System.out.println(a);
System.out.println(new String(buffer, 0, a));


字节对文件内容的读取::
FileInputStream fis = new FileInputStream(".\\com\\goudan\\io\\TestIo.java");
byte[] buffer = new byte[20];
int len = fis.read(buffer);
while (len != -1) {
System.out.print(new String(buffer,0,len));
len = fis.read(buffer);
}


字符流:::
InputStreamReader reader = new InputStreamReader(System.in);
char[] buffer = new char[9];
int c = reader.read(buffer);
System.out.println(new String(buffer));


BufferedReader对文件流的封装::
FileInputStream fis = new FileInputStream(".\\com\\goudan\\io\\TestIo.java");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}

Scanner对文件流的封装::
Scanner s = new Scanner(new File(".\\com\\goudan\\io\\TestIo.java"));
while (s.hasNext()) {
System.out.println(s.nextLine());
}


文件流:::
1》 JFileChooser jfc = new JFileChooser("."); //文件类型的选择器
File path = null;
int type = jfc.showOpenDialog(null); //打开文件时的选择器
if(type == JFileChooser.APPROVE_OPTION) {
path = jfc.getSelectedFile(); //得到选择的文件路径
}
File fileto = null;
FileInputStream fis = new FileInputStream(path); //文件的输入流,文件的路径为path


int st = jfc.showSaveDialog(null); //保存文件的选择器
if(st == JFileChooser.APPROVE_OPTION) {
fileto = jfc.getSelectedFile();
}
FileOutputStream os = new FileOutputStream(fileto); //文件的输出流,文件的路径为fileto
byte[] buffer = new byte[1024]; //定义缓存的字节数组为1K
int len = fis.read(buffer); //将文件读入字节数组
while (len != -1) {
os.write(buffer,0,len); //将文件写到指定的路径下
len = fis.read(buffer);
}
fis.close();
os.close();



2》
String massage = "sdljfsakdjf;lsakjdfoiusaerlkpowei asedfjiow e";
byte[] messages = message.getBytes(); //将文件转换成字节数组
ByteArrayInputStream bais = new ByteArrayInputStream(messages); //把字节数组转换成字节数组流
FileOutputStream fos = new FileOutputStream(file); //创建文件输出流
byte[] buffer = new byte[10240]; //定义字节字节数组流的缓存读取字节数的大小
int len = bais.read(buffer); //向字节数组里读入文件内容
while (len != -1) {
fos.write(buffer, 0, len); //向文件中写入数据
len = bais.read(buffer);
}
fos.close();
bais.close();


3》
private String read(File file){
StringBuffer message = new StringBuffer(""); //新建字符串缓冲区
try {
Scanner sc = new Scanner(file); //对文件进行读取
while (sc.hasNext()) {
String mess = sc.nextLine();
message.append(mess + "\n"); //把文件内容一条一条放到字符串缓冲区内
}
sc.close(); //流的关闭
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return message + ""; //把文件内容以字符串形式返回
}

对象流:::
如果是自定义对象是,要让对象实现Serializable接口(序列化对象的接口)
多个对象分别存储,每次只能读取第一个对象,
1》 对象的输入流
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println(ois.readObject());
2》 对象的输出流
FileOutputStream fos = new FileOutputStream(file,true); //true是将data加在文件的末尾,而不是覆盖前面的数据
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(data);
oos.close();


pipe管道流:::

final PipedInputStream pis1 = new PipedInputStream(); //管道输入流
final PipedOutputStream pos1 = new PipedOutputStream(pis1); //管道输出流


final PipedInputStream pis2 = new PipedInputStream();
final PipedOutputStream pos2 = new PipedOutputStream(pis2);

Thread get = new Thread(){
public void run() {
DataInputStream dis = new DataInputStream(pis2);
while (true) {
try {
double num = dis.readDouble();
System.out.println("read:" + num);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
};

//搬运线程
Thread trans = new Thread(){
public void run(){
DataInputStream dis = new DataInputStream(pis1);
DataOutputStream dos = new DataOutputStream(pos2);
while ( true) {
try {
double num = dis.readDouble();
dos.writeDouble(num);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};

//产生数据的线程
Thread fang =new Thread(){
public void run(){
while (true) {
double num = Math.random();
DataOutputStream dos = new DataOutputStream(pos1);
try {
System.out.println("put:" + num);
dos.writeDouble(num);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
};

fang.start();
trans.start();
get.start();


大对象的存取:::
public static void read(in

t id ,File file){
Connection con = DBCon.getCon();
try {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select pic from my_pics where id=" + id);
rs.next();
BLOB blob = (BLOB) rs.getBlob(1);
int size = 1024000;
InputStream is = blob.getBinaryStream(size);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file),size);
byte[] buffer = new byte[size];
int len = is.read(buffer);
while (len != -1) {
bos.write(buffer, 0, len);
len = is.read(buffer);
}
bos.close();
is.close();


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBCon.close(con);
}
}

public static void write(int id,File file){
Connection con = DBCon.getCon();
try {
con.setAutoCommit(false);
PreparedStatement ps = con.prepareStatement("insert into my_pics values(?,empty_blob())");
ps.setInt(1, id);
ps.executeUpdate();

String sql = "select pic from my_pics where id=" + id;
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
rs.next();
BLOB blob = (BLOB) rs.getBlob(1);
int size = 1024000;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), size);
OutputStream os = blob.setBinaryStream(size);

byte[] buffer = new byte[size];
int len = bis.read(buffer);
while (len != -1) {
os.write(buffer, 0, len);
len = bis.read(buffer);
}

os.close();
bis.close();
https://www.360docs.net/doc/3b3522990.html,mit();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
con.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBCon.close(con);
}
}


zip流:::

File file = new File("e:\\aaaa\\ssss.zip");
try {
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
ZipEntry ze = zis.getNextEntry();

while (ze != null) {
if (!ze.isDirectory()) {

System.out.println(ze.getName() + ":" + ze.getSize());
if (ze.getName().substring(ze.getName().lastIndexOf('.') + 1).equals("zip")){
ZipInputStream nzis = new ZipInputStream(zis);
ZipEntry nze = nzis.getNextEntry();
while (nze != null) {
System.out.println("------>" + nze.getName());
nze = nzis.getNextEntry();
}
}
/*if (ze.getName().equals("src/Test.java")) {

Scanner s = new Scanner(zis);

while (s.hasNext()) {
System.out.println(s.nextLine());
}
}*/
}




ze = zis.getNextEntry();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}















相关文档
最新文档