Beginning Programming with Java for Dummies 2nd phần 7

Với cách tinh nghịch của tôi, tôi đã cố gắng một thử nghiệm nhỏ. Tôi tự hỏi điều gì sẽ xảy ra nếu tôi chạy chương trình cùng một tập tin văn bản nhiều hơn một lần. Vì vậy, tôi tạo ra một chương trình nhỏ (chương trình trong Ví dụ 13-5) và tôi chạy chương trình hai lần. | 230 Part III Controlling the Flow Writing Rewriting and Re-rewriting Given my mischievous ways I tried a little experiment. I asked myself what would happen if I ran the same file-writing program more than once. So I created a tiny program the program in Listing 13-5 and I ran the program twice. Then I examined the program s output file. The output file shown in Figure 13-8 contains only two letters. Listing 13-5 A Little Experiment import import import class WriteOK public static void main String args throws FileNotFoundException PrintStream diskWriter new PrintStream new O K Figure 13-8 Testing the waters. Here s the sequence of events from the start to the end of the experiment 1. Before I run the code in Listing 13-5 my computer s hard drive has no file. That s okay. Every experiment has to start somewhere. 2. I run the code in Listing 13-5. The call to new PrintStream in Listing 13-5 creates a file named . Initially the new file contains no characters. Later in Listing 13-5 calls to print and println put characters in the file. So after running the code the file contains two letters the letters OK. Chapter 13 Piles of Files Dealing with Information Overload 231 3. I run the code from Listing 13-5 a second time. At this point I could imagine seeing OKOK in the file. But that s not what I see in Figure 13-8. After running the code twice the file contains just one OK. Here s why The call to new Printstream in Listing 13-5 deletes my existing file. The call creates a new empty file. After creating a new file the print method call drops the letter O into the new file. The println method call adds the letter K to the same file. So that s the story. Each time you run the program

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.