java虚拟机读写其它进程的数据及RandomAccessFile

本文介绍了Java中如何通过Process类实现父进程与子进程之间的数据交换,包括使用输入流、输出流及错误流进行通信的方法。同时,还探讨了RandomAccessFile类的功能,如在文件任意位置插入数据及向文件末尾追加内容等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值