Although the sparrow is small, it is difficult: Debug is a strange small problem.

Source: Internet
Author: User
Tags ultraedit

The last time the sparrow was small and dirty
"Very popular. I will write another one today, but this time I will talk about the debugging process of a small problem.

Today, a colleague told me that her Java code, if the output file suffix is. idx,
The written line breaks will be lost, and all content will appear in one row from editplus.

I am surprised that the file name and suffix will affect the content of the stream? This is obviously impossible.
Is it true that we have encountered a spiritual event?
Debug and solve the problem as follows:

1,Write independent test code

First, verify that this problem is not a Java issue. The test code is immediately written:

Java code
   import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter; public class TestIdx {    public static void main(String[] args) throws Exception{       File f=new File("d://test.idx");       BufferedWriter w=new BufferedWriter(new FileWriter(f));       w.write("abc");       w.write("/n");       w.write("123");       w.flush();       w.close();    }}

After running, use ultraedit to open the result file. No problem.
My colleague said that she directly used filewriter, so she made some changes and tested it again. No problem.
My colleague also said that there is still no problem in opening, modifying, and testing her files in append mode.

2,REPRODUCE THE PROBLEM

I felt a little incredible, so I went to my colleague's machine to check it.
It is found that this phenomenon exists.

3,Code Reading

Check the code and no special features are found.

4,Perform local replacement and try to locate the problem

By modifying the output file name and file suffix, we tried multiple times and found that the problem was not caused by the. idx suffix,
This is not the case for any newly created files, but it will not work if you continue to append an existing log. idx to her machine.

5,Try to reproduce the problem after cleaning the environment

Since the problem is related to a specific file, delete the strange file (back up the file before deletion, and investigate the root cause after the problem is resolved ),
Run the program again to generate a new file with the same name, and the problem is not reproduced.
The root cause of the problem must be in this file.

6,Tracing

I asked my colleagues to send the log. idx file to me. I opened it with ultraedit and found that the line feed was correct,
When a colleague opens the same file with editplus on the machine, the line feed is incorrect.
So I found another colleague who used editplus to open the file, and the line feed was incorrect.
Therefore, it can be noted that there is no major problem with the file itself. The problem is that the text editor handles the line breaks in the text differently.

Further use the binary mode of ultraedit to study the text file,
The text file contains two line feed formats: "/R/N" for Windows and "/N" for Unix lines ".
It is preliminarily determined that this is the cause of the difference. editplus may only recognize one type of files with two carriage return formats.

7,Verify speculation

Create a blank text file in ultraedit and enter:
123
456
789


Switch to the binary mode in ue, and the display is:
00000000 H: 31 32 33 0d 0a 34 35 36 0d 0a 37 38 39; 123... 456

Open with editplus, normally displayed as three rows

In ue binary mode, delete the second 0d and change it:
00000000 H: 31 32 33 0d 0a 34 35 36 0a 37 38 39; 123 .. 456.789

Open with editplus. The carriage return is not recognized and displayed as two rows:
123
456789

Delete the first 0d and change it:
00000000 H: 31 32 33 0a 34 35 36 0a 37 38 39; 123.456.789

Open with editplus, normally displayed as three rows
123
456
789

Then restore the second 0d:
00000000 H: 31 32 33 0a 34 35 36 0d 0a 37 38 39; 123. 456 .. 789

Open with editplus. The previous carriage return is not recognized and displayed as two rows:
123456
789

8,Conclusion

Editplus determines the line feed of text files,
Editplus can correctly recognize only one line feed format (no matter/R/N or/n) in a text file.
When there are two line feed formats in a text file,/R/N is given priority, and pure/N is ignored.

9,The process of this phenomenon

The newline appended to the file by the first code of this colleague is "/R/N" and later changed to "/N", but the original file was not deleted,
The file write mode is append, resulting in two line feed formats in the file.

Some people may stop after step 6, but I like the next three steps.

 

Conclusion:

Bao110908
The reply explains in detail the processing mechanism of editplus for line feed:


Editplus has a file format to distinguish one of the three line breaks.

1./R/N and/N (that is, a mix of PC and Unix line breaks)
1. If the number of/R/N in the text is greater than/N, it is displayed in the/R/n mode of PC. At this time,/n cannot be displayed as a new line, instead, it becomes a non-explicit character;
2. If the number of/R/N in the text is smaller than/N, it will be displayed in UNIX/n mode, in this case,/R in/R/N becomes a non-explicit character, while/N after/R performs a line break;
3. If the number of/R/N in the text is the same as that of/N,/R/N is preferred, at this time, separate/N becomes a non-explicit character.

2. Mixed/N and/R (that is, mixed UNIX and Mac line breaks)
1. If the number of/N in the text is greater than the number of/R, the line feed is carried out in the Unix/n mode. In this case,/R becomes a non-explicit character;
2. If the number of "/n" in the text is smaller than the number of "/R", the line feed will be carried out in the MAC/r mode. At this time, "/N" will become a non-explicit character;
3. If the number of "/N" and "/n" in the text is the same as that of "/N", the UNIX/n mode is preferred. At the same time, "/R" becomes a non-explicit character.

3. Hybrid use of/R/N and/R (that is, hybrid use of PC and Mac)
1. If the number of/R/N in the text is greater than the number of/R, the/R/n mode of the PC will be used for line feed. In this case,/R will become a non-explicit character;
2. If the number of/R/N in the text is smaller than the number of/R, line feed will be performed in MAC/r mode, And/R/N in the text will become a line break, the following/N is a non-explicit character;
3. If the number of/R/N in the text is the same as that of/R, the/R/n mode of PC is preferred. Separate/R becomes a non-explicit character.

4. Mixed/R/N,/N and/R (that is, a mix of PC, UNIX, and Mac line breaks)
1. Which of the three types of line breaks can be used when there are many line breaks;
2. If three or two types of line breaks are the same, determine the line breaks in the/R/N,/N,/R precedence.

Lab tools
Text display: editplus 3.00 (254) Beta
Binary Modification: ps34.5.3 (2298)

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.