java输入输出学习笔记
2011-06-17 18:00
489 查看
File 类定义了与平台无关的方法来操作文件。java的目录被当成一种特殊的文件,list方法返回目录中的所有目录和文件名。DOS下的路径分隔符为/,Unix下的路径分隔符为/,java都可识别。
createNewFile必须用try catch,抛出异常。getParent与创建的File对象有关,注意转义字符的使用,lastModified()的返回值是long型的,可以创建Date对象来输出具体时间,注意引入util.Date包。
RandomAccessFile类随机访问文件。根据构造函数来确定读写模式。个人感觉对于文字还是用writeUTF(),readUTF()比较方便。
String 的trim()方法越过空格.
对inputStream与OutputStream,FileInputStream与FileOutputStream,Reader与Writer,PipedInputStream与PipedOutputStream,ByteArayInputSteam与ByteArayOutputSteam类的使用。文件是数据的静态存储形式,而流是数据传输的形态。
inputStream 中的read()方法返回值为int,最大值255,最小值1,不会有-1出现,与结束流的标记-1进行区别,虽然有垃圾回收器,但是一定不可以省略close方法,系统本身会产生资源,垃圾回收器只能回收JAVA中的资源,无法回收系统资源。
OutputStream中的write(int)方法将最低字节写入,最高字节被舍弃,flush()用于将缓冲区内容强制写入设备,清空内存缓冲区。
FileInputStream创建磁盘文件输入流,如果文件存在,这个文件中的内容将被覆盖,输入输出是针对程序而言的,
PipedInputStream与PipedOutputStream类用于创建管道通信。实现模块间的通信。
ByteArayInputSteam和ByteArayOutputSteam完成对字节数组内容的读写,来支持类似内存虚拟文件功能。
管道有一定的容量,如果缓冲区满了后,就会造成数据传输有误.
一下代码应该要细看
import java.io.*;
public class testinout implements Runnable
{
Process p=null;
public testinout ()
{
System.out.println("testinout构造函数");
try{
p=Runtime.getRuntime().exec("java mythread");
new Thread(this).start();
}catch(Exception e) {}
}
public static void main(String args[])
{
testinout n=new testinout ();
n.send();
System.out.println("testinout执行到最后");
}
public void send()
{
System.out.println("send函数已经调用");
try{
OutputStream ops=p.getOutputStream();
while(true)
{
ops.write("help/r".getBytes());
}
}catch(Exception e) {}
}
public void run()
{
System.out.println("开始run");try{
while(true)
{InputStream in=p.getInputStream();
BufferedReader bfr=new BufferedReader(new InputStreamReader(in));
String strline=bfr.readLine();
if(strline!=null)
System.out.println(strline);
else
return;
}
}catch(Exception e) {}
}
}
class mythread
{
public static void main(String args[])throws Exception
{
System.out.println("mythread");
while(true)
{
String s=new BufferedReader(new InputStreamReader(System.in)).readLine();
if(s!=null)
System.out.println("hi:!"+new BufferedReader(new InputStreamReader(System.in)).readLine());
else
return;
}
}
}
createNewFile必须用try catch,抛出异常。getParent与创建的File对象有关,注意转义字符的使用,lastModified()的返回值是long型的,可以创建Date对象来输出具体时间,注意引入util.Date包。
RandomAccessFile类随机访问文件。根据构造函数来确定读写模式。个人感觉对于文字还是用writeUTF(),readUTF()比较方便。
String 的trim()方法越过空格.
对inputStream与OutputStream,FileInputStream与FileOutputStream,Reader与Writer,PipedInputStream与PipedOutputStream,ByteArayInputSteam与ByteArayOutputSteam类的使用。文件是数据的静态存储形式,而流是数据传输的形态。
inputStream 中的read()方法返回值为int,最大值255,最小值1,不会有-1出现,与结束流的标记-1进行区别,虽然有垃圾回收器,但是一定不可以省略close方法,系统本身会产生资源,垃圾回收器只能回收JAVA中的资源,无法回收系统资源。
OutputStream中的write(int)方法将最低字节写入,最高字节被舍弃,flush()用于将缓冲区内容强制写入设备,清空内存缓冲区。
FileInputStream创建磁盘文件输入流,如果文件存在,这个文件中的内容将被覆盖,输入输出是针对程序而言的,
PipedInputStream与PipedOutputStream类用于创建管道通信。实现模块间的通信。
ByteArayInputSteam和ByteArayOutputSteam完成对字节数组内容的读写,来支持类似内存虚拟文件功能。
管道有一定的容量,如果缓冲区满了后,就会造成数据传输有误.
一下代码应该要细看
import java.io.*;
public class testinout implements Runnable
{
Process p=null;
public testinout ()
{
System.out.println("testinout构造函数");
try{
p=Runtime.getRuntime().exec("java mythread");
new Thread(this).start();
}catch(Exception e) {}
}
public static void main(String args[])
{
testinout n=new testinout ();
n.send();
System.out.println("testinout执行到最后");
}
public void send()
{
System.out.println("send函数已经调用");
try{
OutputStream ops=p.getOutputStream();
while(true)
{
ops.write("help/r".getBytes());
}
}catch(Exception e) {}
}
public void run()
{
System.out.println("开始run");try{
while(true)
{InputStream in=p.getInputStream();
BufferedReader bfr=new BufferedReader(new InputStreamReader(in));
String strline=bfr.readLine();
if(strline!=null)
System.out.println(strline);
else
return;
}
}catch(Exception e) {}
}
}
class mythread
{
public static void main(String args[])throws Exception
{
System.out.println("mythread");
while(true)
{
String s=new BufferedReader(new InputStreamReader(System.in)).readLine();
if(s!=null)
System.out.println("hi:!"+new BufferedReader(new InputStreamReader(System.in)).readLine());
else
return;
}
}
}
相关文章推荐
- JAVA高级视频02_IO输入与输出 06 ByteArrayInputStram和ByteArrayOutputStream 学习笔记
- Java学习笔记:输入、输出数据
- JAVA高级视频_IO输入与输出 Java程序与其他进程的数据通讯 学习笔记
- 黑马训练营java学习笔记:输入/输出
- java学习笔记IO输入与输出——IO字符流(3)
- [学习笔记]Java输入输出和流程控制
- java学习笔记IO输入与输出——常用工具类(1)
- java输入输出学习笔记2
- JAVA输入/输出系统中的其他流学习笔记
- Java学习笔记(9): 输入/输出
- java学习笔记IO输入与输出——File类及IO其它类(4)
- java文件输入输出流学习笔记
- JAVA高级视频02_IO输入与输出 06 PipedInputStream和PipedOutPutStream 学习笔记
- Java学习笔记―第十章 数据输入与输出
- Java学习笔记--输入/输出
- JAVA高级视频_IO输入与输出04 学习笔记
- 黑马程序员——Java学习笔记之⑤——“IO输入与输出”
- java学习笔记IO输入与输出——IO流概述及字节流(2)
- JAVA高级视频_IO输入与输出01 学习笔记
- JAVA输入输出流学习笔记