java虚拟机读写其它进程的数据及RandomAccessFile
来源:互联网 发布:捕蜂器淘宝 编辑:程序博客网 时间:2024/05/17 21:55
1.java的Process类提供了如下3个方法用于让程序和其子进程进行通信。
InputStream getErrorStream():获取子进程的错误流。
InputStream getInputStream():获取子进程的输入流(将子进程的数据输出给本进程,作为本进程的输入)。
OutputStream getOutputStream():获取子进程的输出流(即将数据输出给子进程,作为子进程的输入)。如下:
import java.io.*;import java.util.*;class WriteToProcess {public static void main(String[] args) throws IOException{Process p=Runtime.getRuntime().exec("java ReadStandard");//运行java ReadStandard命令,并返回该命令的子进程try(PrintStream ps=new PrintStream(p.getOutputStream()))//获取子进程的输出流,对本进程时输出,对子进程是输入流{ps.println("普通字符");}}}class ReadStandard {public static void main(String[] args) {try(PrintStream ps=new PrintStream(new FileOutputStream("out1.txt"))){Scanner sc=new Scanner(System.in);//获取标准输入,该输入来自WriteToProcess方法sc.useDelimiter("\n");while(sc.hasNext()){ps.println(sc.next());}}catch(IOException ex){ex.printStackTrace();}}}
2.RandomAccessFile类可以自由访问文件的任意位置
构造器:RandomAccessFile(File file, String mode)和RandomAccessFile(String name, String mode),其中mode参数用来指定文件的访问模式,该参数有如下4个值:
"r":只读方式打开文件, ”rw“:读写方式,"rws":以读写方式打开,但要求对文件的内容或元数据同步写入底层设备,"rwd"”:读写方式打开,要求对文件的内容同步写入底层设备。(后两者没看懂)。
import java.io.*;class RandomAccessFileTest{ public static void insert(String filename,long pos,String inscontent) throws IOException{File tmp=File.createTempFile("tmp",null);//创建一个临时文件,用于保存插入点之后的内容tmp.deleteOnExit();try(RandomAccessFile raf=new RandomAccessFile(filename,"rw"); FileOutputStream tmpout=new FileOutputStream(tmp);FileInputStream tmpin=new FileInputStream(tmp)){raf.seek(pos);//定位到插入点byte[] buff=new byte[64];int hasRead=0;while((hasRead=raf.read(buff))>0){//将插入点之后的内容保存到tmp文件中tmpout.write(buff,0,hasRead);}raf.seek(pos);raf.write(inscontent.getBytes());//添加要插入的内容while((hasRead=tmpin.read(buff))>0){//将tmp文件中的内容追加到文件中raf.write(buff,0,hasRead);}}}public static void main(String[] args) throws IOException{try(RandomAccessFile raf=new RandomAccessFile("RandomAccessFileTest.java","r")){System.out.println("起始位置:"+raf.getFilePointer());raf.seek(200);//从200位置开始读取文件内容byte[] buff=new byte[1024];int hasRead=0;while((hasRead=raf.read(buff))>0){System.out.println(new String(buff,0,hasRead));}}catch(IOException ex){ex.printStackTrace();}try(RandomAccessFile rafread=new RandomAccessFile("out2.txt","rw")){rafread.seek(rafread.length());//定位到文件结尾rafread.write("追加的内容:".getBytes());//添加内容}catch(IOException ex){ex.printStackTrace();}insert("RandomAccessFileTest.java",45,"123\r\n");}}
0 0
- java虚拟机读写其它进程的数据及RandomAccessFile
- Java虚拟机读写其他进程的数据
- Java虚拟机读写其他进程的数据
- IO:Java虚拟机读写其他进程的数据(1)
- JAVA虚拟机读写其他进程数据
- JAVA输入输出(6)--Java虚拟机读写其他进程的数据
- [疯狂Java]I/O:标准流重定向、JVM读写其它进程数据
- java文件随机读写--RandomAccessFile
- Java使用RandomAccessFile读写文件
- java:RandomAccessFile(随机读写文件)
- java RandomAccessFile随机读写流
- Java使用RandomAccessFile读写文本文件
- Java的RandomAccessFile随机文件读写的简单使用
- Java的随机读写文件-RandomAccessFile 与内存映射
- Java的随机读写文件-RandomAccessFile 与内存映射
- Java的随机读写文件-RandomAccessFile 与内存映射
- 关于二进制文件的读写RandomAccessFile
- RandomAccessFile 对文件的读写
- 蜗牛—cocos2dx之HelloWorld
- Nginx部署部分https与部分http
- vs2010中的Quick Search
- Leetcode Maximum Subarray
- hud 1532 Drainage Ditches + 网络流入门题
- java虚拟机读写其它进程的数据及RandomAccessFile
- Selenium2.0功能测试之唯一元素的定位
- 山东理工大学ACM平台题答案关于C语言 1196 排序问题
- Hadoop2.2.0的Eclipse插件
- javascript中实现继承的示例
- STL Vector
- eclipse中重构技巧
- ASIC的一些技巧和软件
- 基于HTML5技术的电力3D监控应用(二)