/**
* File类
* 代表磁盘文件本身信息的类,而不是文件中的内容
* 操作:创建,删除、重命名
* Unix下路径分隔符为/,在DOs下为\
*
*/
File f = new File("file.txt");
if(f.exists())
{
f.delete();
}else{
try {
System.out.println(f.createNewFile());
System.out.println(f.getName());
System.out.println(f.getAbsoluteFile());
System.out.println(f.getAbsolutePath());
System.out.println(f.getParent());
System.out.println(f.getParentFile());
System.out.println(f.getTotalSpace());
System.out.println(f.getPath());
System.out.println(f.canExecute());
System.out.println(f.canRead());
System.out.println(f.canWrite());
System.out.println(f.isAbsolute());
System.out.println(f.isDirectory());
System.out.println(f.isFile());
System.out.println(f.isHidden());
System.out.println(f.renameTo(new File("file2.txt")));
System.out.println(f.lastModified());
System.out.println(new Date(f.lastModified()));
System.out.println(f.toURL());
System.out.println(f.toURL());
} catch (IOException e) {
e.printStackTrace();
}
/**
* Reader和Writer都是字符流类的抽象基类
* 二进制文件和文本文件的区别
* 字节中数据为0-255,都存放在内存中,文件中的数据也是二进制的
* 文本字符是由多个字节组成的
*
* FileWriter
* FileReader
*
* @author ShenJie
*
*/
//txt是普通ASCII文件,每个字符占一个字节,
//java中字符串每个字符占两个字节,可以实现内部格式到外部磁盘文件格式的转换
String fileName = "C:\\Hello.txt";
//写入更多使用BufferedWriter
FileWriter out = new FileWriter(fileName,true);
out.write("Hello\n");
out.write("你好啊\n");
out.close();
BufferedReader in = new BufferedReader(
new FileReader(fileName));
String line;
line = in.readLine();
while(line != null)
{
System.out.println(line);
line = in.readLine();
}
in.close();
//BufferedReader缓冲
//InputStreamReader字节流向字符流转换
BufferedReader bfin = new BufferedReader(
new InputStreamReader(System.in));
String s;
while((s = bfin.readLine()).length() != 0)
System.out.println(s);
// 用友好方式显示提示的输入信息的要求
public static void ShowDialogPane(){
String name=JOptionPane.showInputDialog("What is your name?");
String input=JOptionPane.showInputDialog("How old are you?");
int age=Integer.parseInt(input);
System.out.println("Hello, "+name+", Next year, you'll be "+(age+1));
}
/**
* 理解流的概念
* 流是字节序列的抽象概念
* 文件时数据静态存储形式,流是数据传输时的形态
* 节点流类和过滤流类
*
* InputStream and OutputSream
* int read()
* int read(byte[] b)
* int read(byte[] b, int off, int len)
* long skip(long n)
* int available() 流中有数据,再读取
*
* void write(int b)
* void write(byte[] b)
* void write(byte[]b, int off, intlen)
* void flush() //内存缓冲区中内容彻底清空,并且输入到
*
* FileInputStream FileOutputStream
* 创建磁盘文件
* 读应该存在,写可以不存在
* FileInputStream inOne = new FileInputStream("hello.test");
*
* File f= new File("hello.test");
* FileInputStream inTwo = new FileInputStram(f);
*
*
* @author ShenJie
*
*/
public static void main(String[] args){
try {
FileOutputStream out = new FileOutputStream("fos.a");
try {
//write写入字节数组时自动调用flush,写入文件中,可以不用close
out.write("沈杰".getBytes());
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
FileInputStream in = new FileInputStream(new File("fos.a"));
byte[] buf = new byte[1024];
int len = 0;
try {
len = in.read(buf, 0 , buf.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(new String(buf,0,len));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 大的数据缓冲区
* ByteArrayInputStream和ByteArrayOutputStream,
* 用于IO流的方式来完成对字节数组内容的读写,
* 支持类似内存虚拟文件或者内存映像文件的功能
*
* ByteArrayInputStream(byte[] buf)
* ByteArrayInputStream(byte[] buf, int offset, int length)
* ByteArrayOutputStream() // 32个字节的缓冲区
* ByteArrayOutputStream(int) //根据大小
*
* System.in是InputStream类型的实例对象
* System.out是PrintStream类的实例对象
* 不论文件流和网络流,InputStream的read方法返回-1表示输入流的结束
* Ctrl+Z输入流结束/Ctrl+D
*
* 把一个输入流中所有的英文字母编程大写字母
* 将结果写入到输出流对象
* 【类似压缩】
* @author ShenJie
*
*/
public static void main(String[] args){
String tmp = "dasfascfasdf沈杰";
byte[] src = tmp.getBytes();
ByteArrayInputStream input = new ByteArrayInputStream(src);
ByteArrayOutputStream output = new ByteArrayOutputStream();
transfrom(input, output);
byte[] result = output.toByteArray();
System.out.println(new String(result));
transfrom(System.in, System.out);
}
public static void transfrom(InputStream in, OutputStream out){
//输入流中数据读取出进行转换
int ch;
try {
while((ch = in.read()) != -1){
// int upperCh = Character.toUpperCase(ch);
int upperCh = ch;
out.write(upperCh);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 包装类概念与作用
* FileOutputStream把浮点小数写入文本中
*
* DataOutputStream类提供了各种输出流对象中写入各种类型的数据
* 传递一个具体流设备的输出流对象
* public DataOutputStream(OutputStream out)
*
* 包装类 节点流类
* 程序 方法一 方法一 目标
* 方法二
*
* BufferedInputStream
* BufferedOutputStream 缓冲流为I/O流增加了内存缓冲区,一次不止操作一个字节,可以执行skip、mark、reset
* 底层系统缓冲区直接与目标连接、往硬盘读或者写、一下读很多,包装类缓冲区读写底层缓冲区、一下读一个
*
* BufferedInputStream(INputStream in)
* BufferedInputStream(INputStream in, int size)
*
* BufferedReader的readLine可以一次读取一行文本
*
* ==DataOutputStream==
* public final void writeBytes(String s)低字节
* public final void writeChars(String s)
* public final void writeUTF(String s)
*
* DataOutputStream BufferedInputStream FileOutputStream
* 程序 文件
*
* 关闭最上层流对象,自动关闭底层流对象
*
*
* @author ShenJie
*
*/
public static void main(String[] args) throws Exception{
FileOutputStream fos = new FileOutputStream("fos.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF("ab中国");
dos.writeBytes("ab中国");
dos.writeChars("ab中国");
dos.close();
FileInputStream fis = new FileInputStream("fos.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
System.out.println(dis.readUTF());
byte[] buf = new byte[1024];
int len = dis.read(buf);
System.out.println(new String(buf, 0, len));
dis.close();
}
//
String fn = "c:/data.dat";
DataOutputStream dops;
try {
dops = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(fn)));
dops.writeInt(1);
System.out.println(dops.size() + " bytes has been written!");
dops.writeInt(76);
System.out.println(dops.size() + " bytes has been written!");
dops.close();
} catch (Exception e) {
e.printStackTrace();
}
//
DataInputStream dis;
try {
dis = new DataInputStream(
new FileInputStream(fileName));
int l = dis.readInt();
System.out.println(l);
l = dis.readInt();
System.out.println(l);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
/*** PrintStream(OutputStream out)
* PrintStream(OutputStream out,boolean autoflush) //自动刷新缓冲区
* PrintStream(OutputStream out,boolean autoflush, String encoding)
*
* 换行符 windows下\r\n linux下\n
*
* ObjectInputStream
* ObjectOuputStream
* 从底层输入流中读取对象类型的数据
*
* 创建一个可序列化的对象,存储并恢复
* Student
*
* 字节字符设备读取减少转化
*
*
* @author ShenJie
*
*/
/**
* TestInOut类中启动java.exe命令执行一个MyTest类,TestInOut和MyTest通过进程间管道相互传递数据
*
* @param args
*/
Process p =null;
IODemo11(){
try {
p = Runtime.getRuntime().exec("java MyTest");
} catch (IOException e) {
e.printStackTrace();
}
new Thread(this).start();
}
public static void main(String[] args) {
IODemo11 i = new IODemo11();
i.send();
}
public void send(){
//发送子进程数据
OutputStream ops = p.getOutputStream();
while(true){
try {
ops.write("help\r\n".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
//接收子进程数据
InputStream in = p.getInputStream();
BufferedReader brf = new BufferedReader(
new InputStreamReader(in));
String strLine = null;
try {
strLine = brf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(strLine);
}
// 对象序列化输出信息
public static void OutputInfo(){
String[] students = new String[5];
try{
FileOutputStream fo = new FileOutputStream("stu.ser");
ObjectOutputStream so = new ObjectOutputStream(fo);
for(int k=0;k<5;k++)
so.writeObject(students[k]);
so.close();
}catch(Exception e){
System.out.print(e);
}
}
// 对象序列化输入信息
public static void InputInfo(){
String[] students = new String[5];
try{
FileInputStream fi = new FileInputStream("stu.ser");
ObjectInputStream si = new ObjectInputStream(fi);
for(int k=0;k<5;k++)
students[k] = (String)si.readObject();
si.close();
}catch(Exception e){
System.out.print(e);
}
}