`
bachelor007
  • 浏览: 55635 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Read and Write Files

阅读更多
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();
 }

}

分享到:
评论

相关推荐

    read and write BLF logging files.rar

    CAN_and_General_BLF_Format.pdf Ethernet_BLF_Format.pdf FlexRay_BLF_Format.pdf LIN_BLF_Format.pdf MOST_BLF_Format.pdf TPDiag_BLF_Format.pdf binlog.dll The document specifies the format of CAN/Ethernet/...

    Compressed Image File Formats JPEG, PNG, GIF, XBM, BMP

    can read and write files using various 2-D image formats. I wanted to write a book that explains the most frequently used file formats with enough depth for the reader to implement them, as opposed to...

    R.matlab Read and Write MAT Files and Call MATLAB from

    标题 "R.matlab Read and Write MAT Files and Call MATLAB from Within R" 描述的是在R语言环境中如何与MATLAB进行交互,主要包括读写MAT文件以及在R中调用MATLAB的功能。MAT文件是MATLAB用于存储变量的标准格式,...

    Matlab scripts to read and write MIDI files.zip

    标题 "Matlab scripts to read and write MIDI files" 暗示了这个压缩包包含一系列MATLAB脚本,这些脚本专门用于处理MIDI(Musical Instrument Digital Interface)文件。MIDI是一种数字音频标准,用于电子乐器、...

    Linux Programming Interface

    Read and write files efficiently Use signals, clocks, and timers Create processes and execute programs Write secure programs Write multithreaded programs using POSIX threads Build and use shared ...

    R.matlab Read and Write MAT Files and Call

    在R语言中,与MATLAB进行交互是许多数据科学家和工程师的需求,因为这两个环境各有优势。`R.matlab`包提供了在R环境中读写MAT文件以及直接调用MATLAB功能的功能,极大地拓展了R的数据处理和分析能力。...

    Small.Sharp.Software.Tools.epub

    Turn tedious chores into quick tasks: read and write files, manage complex directory hierarchies, perform network diagnostics, download files, work with APIs, and combine individual programs to ...

    Programming Excel With Vba And .net.chm

    Read and Write Files Section 3.7. Check Results Section 3.8. Find Truth Section 3.9. Compare Bits Section 3.10. Run Other Applications Section 3.11. Control the Compiler Section 3.12. Not...

    VC_class_read_write_INI_files_sample_programs.rar_INI VC_read .i

    描述"VC++读写INI文件类以及示例程序VC + + class to read and write INI files and sample programs"表明,这个压缩包里可能包含了一个C++类,用于读取和写入INI文件,以及相关的示例代码,帮助开发者理解和使用这...

    android-read-write.rar_android_android read write_android write

    "android-read-write.rar"这个压缩包文件,结合其标题和描述,显然关注的是如何在Android环境中进行文件的读取和写入操作。下面我们将深入探讨Android系统的文件存储机制以及具体的读写操作方法。 首先,Android为...

    NativeJpg 控件

    to read and write Jpeg files. It provides a fully object-oriented approach to working with Jpeg files, with clearly defined properties, events and methods. You can use this code to read and write ...

    Java_programming_code_read_write_files_classic.rar_java programm

    String content = new String(Files.readAllBytes(Paths.get("file.txt")), StandardCharsets.UTF_8); ``` 5. **文件追加** 如果想在文件末尾追加内容,可以使用`FileWriter`的构造函数传入`true`参数: ```...

    WINIMAGE.V.6.1.SDK.WITH.VC.VB.DELPHI.SAMPLE

    Using the WinImage SDK, you can create Win32 applications that read and write floppies, read and write floppy disk image files, extract files from image files, inject files into image files, and offe...

    nativejpg v1.24

    to read and write Jpeg files. It provides a fully object-oriented approach to working with Jpeg files, with clearly defined properties, events and methods. You can use this code to read and write ...

    Library to read and write Paradox files-开源

    Paradox是一种早期的数据库文件格式,由Borland公司开发,主要用于DOS系统下的数据库管理。随着技术的发展,虽然Paradox不再是最主流的数据库格式,但在一些旧的系统和项目中仍然存在。开源库"pxlib"是为了解决与...

    Experimental code to readwrite NumPy .NPY files in MATLAB.zip

    标题中的“Experimental code to readwrite NumPy .NPY files in MATLAB”表明这是一段用于在MATLAB环境中读取和写入NumPy的.NPY格式文件的实验性代码。NumPy是Python的一个科学计算库,其.NPY格式是NumPy保存多维...

    Great .Bas Module file which can enables you to Read Write I

    Great .Bas Module file which can enables you to Read Write INI files Read Write the Registry a Numerical Encrypter Decrypter which will encrypt strings of data into numbers and back and a customizable...

    LibXL.for.Windows.3.6.5.0.Incl.License.and.Cracked

    LibXL is a library that can read and write Excel files. It doesn't require Microsoft Excel and .NET framework, combines an easy to use and powerful features. Library can be used to •Generate a new ...

Global site tag (gtag.js) - Google Analytics