Read and write operations on files in Java __java

Source: Internet
Author: User
Like we often encounter such things, such as a TXT file with a name and telephone, this time very often need to be the name and phone number to extract operations, this time can be used in Java IO to achieve. Here I do not specifically introduce IO in the byte stream and character streams similarities and differences point, interested students can own Baidu Baidu. Today is mainly about how to achieve the acquisition of the contents of the file is to obtain the contents of the file to modify the operation. Look at the specific case introduction. This is the end of the case to achieve the effect of the name and phone number directly add a split symbol. Here's a bit of a need the main thing is that this case is not directly in the original TXT document modified above, but a new TXT file to write new content. Well, the nonsense is not much to say, to see how the case is specifically implemented. This case is divided into three modules: 1. File reading module, 2 name telephone separation module, 3. File Write module 1. File reading module:
 /** * Function: Java read TXT file content * Step: 1: First get file handle * 2: Get file handle as input a byte stream, you need to read the input stream * 3: Read to the input stream, you need to read the generated byte * 4: One line of output.
     ReadLine (). * Note: Exceptions need to be considered * @param filePath */public static string Readtxtfile (String filePath) {StringBuilder Conten
		t = new StringBuilder ("");
			try {String encoding = "UTF-8";
			File File = new file (FilePath); if (File.isfile () && file.exists ()) {InputStreamReader read = new InputStreamReader (new FileInputStream (file)
				, encoding);
				BufferedReader BufferedReader = new BufferedReader (read);
				String linetxt = null;
					while ((Linetxt = Bufferedreader.readline ())!= null) {string[] result = Getnamephone (linetxt);
					System.out.println (Linetxt);
					Content.append (Result[0] + "----" + result[1]);
			Content.append ("\ r \ n");/txt newline} read.close ();
			else {System.out.println ("The specified file cannot be found");
			The catch (Exception e) {System.out.println ("Error reading file contents");
		E.printstacktrace (); } RETurn content.tostring (); }
2. Name and telephone separation module:
public static string[] Getnamephone (String str) {
		string[] result = new String[2];
		int index = 0;
		for (int i = 0; i < str.length (); i++) {
			if (Str.charat (i) >= ' 0 ' && str.charat (i) <= ' 9 ') {
				Inde x = i;
				break;
			}
		}
		Result[0] = str.substring (0, index);
		RESULT[1] = str.substring (index);
		return result;
	}
3. File Write module:
public static void Printfile (String content) {
		BufferedWriter bw = null;
		try {
			File File = new file ("D:/filename.txt");
			if (!file.exists ()) {
				file.createnewfile ();
			}
			FileWriter FW = new FileWriter (File.getabsolutefile ());
			BW = new BufferedWriter (FW);
			Bw.write (content);
			Bw.close ();
		} catch (IOException e) {
			e.printstacktrace ();
		}
	}
Through these three modules can be implemented to read the file operation, and then processing information, and finally the processing of information to add to the new file. Here need attention is: the coding format of the project to write Utf-8, otherwise there will be garbled situation. Here the file read and write operation is completed, is not particularly simple and convenient. If you have any doubts or questions about the above, you can add me to qq:208017534 consultation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.