Read and Write Files

本文介绍了一个Java程序中实现的基本文件读写操作案例,包括从键盘读取输入、从文件读取内容、从字符串读取内容及将字符串写入文件等步骤。此外,还提供了从输入流读取字符串及向输出流写入字符串的方法。

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

public class ReadWriteFileTest {

	public void opeateFilesSample()
	{
		//read from keyboard
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter a line :");
		try {
			System.out.println(stdin.readLine());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//read from files
		String s1 = null;
		StringBuffer s2 = new StringBuffer();
		try {
			BufferedReader in = new BufferedReader(new FileReader(""));
			while((s1 = in.readLine()) != null)
				s2.append(s1 + "\n");
			
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//read from string
		StringReader in1 = new StringReader(s2.toString());
		int c ;
		try {
			while((c = in1.read()) != -1){
				System.out.println((char)c);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//write string to files
		BufferedReader in2 = new BufferedReader(new StringReader(s2.toString()));
		try {
			PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("")));
			int lineCount = 1;
			while((s1 = in2.readLine()) != null){
				out.println(lineCount++ + s1);
			}
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public String inputSteam2String(InputStream ins, String charset) throws IOException
	{
		BufferedReader reader = new BufferedReader(new InputStreamReader(ins, charset));
		
		StringBuffer buffer = new StringBuffer();
		String line = null;
		
		while((line = reader.readLine()) != null){
			buffer.append(line);
		}
		reader.close();
		return buffer.toString();
	}
	
	public void string2OutputStream(OutputStream os, String s, String charset) throws IOException
	{
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, charset));
		writer.write(s);
		writer.close();
	}
	
	public static void readFiles()
	{
		String s1 = null;
		StringBuffer s2 = new StringBuffer();
		try {
			BufferedReader in = new BufferedReader(new FileReader("D:\\Cich_endi_ESTs_2007_08_10.gb.txt"));
			PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D:\\test.fasta")));
			/*while((s1 = in.readLine()) != null){
				//s2.append(s1 + "\n");
				//out.println(s2);
				out.println(s1);
			}*/
			while((s1 = in.readLine()) != null){
				s2.append(s1 + "\n");
				//out.println(s2);
			}
			out.println(s2);
			
			in.close();
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void rwFiles()
	{
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*String str = "LOCUS       EL372564";
		System.out.println("--String: " + str.length());
		
		StringBuffer stb = new StringBuffer("LOCUS       EL372564                 ");
		System.out.println("--StringBuffer: " + stb.length());*/
		readFiles();
	}

}

 

public class ReadWriteFileTest {

 public void opeateFilesSample()
 {
  //read from keyboard
  BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter a line :");
  try {
   System.out.println(stdin.readLine());
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  //read from files
  String s1 = null;
  StringBuffer s2 = new StringBuffer();
  try {
   BufferedReader in = new BufferedReader(new FileReader(""));
   while((s1 = in.readLine()) != null)
    s2.append(s1 + "\n");
   
   in.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  //read from string
  StringReader in1 = new StringReader(s2.toString());
  int c ;
  try {
   while((c = in1.read()) != -1){
    System.out.println((char)c);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  //write string to files
  BufferedReader in2 = new BufferedReader(new StringReader(s2.toString()));
  try {
   PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("")));
   int lineCount = 1;
   while((s1 = in2.readLine()) != null){
    out.println(lineCount++ + s1);
   }
   out.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 
 public String inputSteam2String(InputStream ins, String charset) throws IOException
 {
  BufferedReader reader = new BufferedReader(new InputStreamReader(ins, charset));
  
  StringBuffer buffer = new StringBuffer();
  String line = null;
  
  while((line = reader.readLine()) != null){
   buffer.append(line);
  }
  reader.close();
  return buffer.toString();
 }
 
 public void string2OutputStream(OutputStream os, String s, String charset) throws IOException
 {
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, charset));
  writer.write(s);
  writer.close();
 }
 
 public static void readFiles()
 {
  String s1 = null;
  StringBuffer s2 = new StringBuffer();
  try {
   BufferedReader in = new BufferedReader(new FileReader("D:\\Cich_endi_ESTs_2007_08_10.gb.txt"));
   PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("D:\\test.fasta")));
   /*while((s1 = in.readLine()) != null){
    //s2.append(s1 + "\n");
    //out.println(s2);
    out.println(s1);
   }*/
   while((s1 = in.readLine()) != null){
    s2.append(s1 + "\n");
    //out.println(s2);
   }
   out.println(s2);
   
   in.close();
   out.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void rwFiles()
 {
  
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  /*String str = "LOCUS       EL372564";
  System.out.println("--String: " + str.length());
  
  StringBuffer stb = new StringBuffer("LOCUS       EL372564                 ");
  System.out.println("--StringBuffer: " + stb.length());*/
  readFiles();
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值