Search Header Logo
Filehand

Filehand

Assessment

Presentation

•

Computers

•

9th - 12th Grade

•

Practice Problem

•

Easy

Created by

Arasaka Teacher

Used 3+ times

FREE Resource

1 Slide • 16 Questions

1

File

By Arasaka Teacher

2

Reorder

String name = "abc.txt";
File file = new File(name);
System.out.println(file.exists());
try {
// 使用 FileReader 打开文件
使用 BufferedReader 包装 FileReader,提高读取效率
// 用于存储从文件中读取的每一行内容
String line;
// 先读取一行

//条件是什么



{
// 输出当前行
System.out.println(line);
// 读取下一行
line = reader.readLine();
}
} ; // 处理异常
}

FileReader fileReader = new FileReader(file);

BufferedReader reader = new BufferedReader(fileReader);

line = reader.readLine();

while (line != null)

catch (IOException e) {
e.printStackTrace()

1
2
3
4
5

3

Drag and Drop

// 1. 使用 File 类创建文件对象

​




// 4. 将内容写入文件

​
// 写入多行内容





​
;

Drag these tiles and drop them in the correct blank above
File file = new File(name);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(content);
File file = new File();
FileWriter fileWriter = new FileWriter();
fileWriter.append(content);
fileWriter.close()

4

Fill in the Blanks

Before performing any file operations, the file must be

in a specified mode.

5

Fill in the Blanks

import java.io.*;

public class ReadData {

public static void main(String[] args) {

String name = "abc.txt"; // 文件名

String line;

try (BufferedReader reader = new BufferedReader(new FileReader(name))) {

int i = 0;

// 在此处填入合适的代码,使循环能读取文件前 10 行

while (

) {

// 读取文件中的每一行

line = reader.readLine();

// 打印出当前行内容

System.out.println(line);

i++; // 计数器增加

}

} catch (IOException e) {

e.printStackTrace(); // 捕获并打印异常

}

}

}

6

Fill in the Blanks

import java.io.*;

public class ReadAndCount {

public static void main(String[] args) {

String fileName = "test.txt";

String line;

int lineCount = 0;

int nonEmptyLineCount = 0;

try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {

while(lineCount < 5) && ((line = reader.readLine()) != null) {

line = reader.readLine();

if (line != null && !line.trim().isEmpty()) {

————————++

}

——————++;

}

System.out.println("非空行数量: " + nonEmptyLineCount);

} catch (IOException e) {

e.printStackTrace();

}

}

}

7

Fill in the Blanks

The command to open a file for reading is written as:

<file identifier> FOR READ.

Answer:

8

Fill in the Blanks

To add data to an existing file without overwriting its contents, the file should be opened in

mode.

Answer:

9

Fill in the Blanks

  • The command used to read the next line of text from a file is

    <file identifier>, <variable>.

    Answer:

10

Fill in the Blanks

The function

is used to check whether there are any more lines to be read from a file.

11

Fill in the Blanks

To close a file after it is no longer needed, use the

command.

Answer:

12

Fill in the Blanks

Data is written into the file (after the file has been opened in WRITE or APPEND mode) using the



13

Reorder

Write several lines of text to the file.

DECLARE textLn : STRING

DECLARE myFile : STRING

myFile ← "myText.txt"

REPEAT

    OUTPUT "Please enter a line of text"

  

IF textLn <> ""

    THEN

       

    ELSE

      

    ENDIF

OUTPUT "The file contains these lines of text:"

OPEN myFile FOR WRITE

  INPUT textLn

 WRITEFILE myFile, textLn

  CLOSEFILE(myFile)

UNTIL textLn = ""

1
2
3
4
5

14

Reorder

This example uses the operations together, to copy all the lines from FileA.txt to FileB.txt,

replacing any blank lines by a line of dashes.

DECLARE LineOfText : STRING

OPENFILE "FileA.txt" FOR READ

OPENFILE "FileB.txt" FOR WRITE

WHILE NOT EOF("FileA.txt")

READFILE "FileA.txt", LineOfText

IF LineOfText = "" THEN

WRITEFILE "FileB.txt", " ----------------------------"

ELSE

WRITEFILE "FileB.txt", LineOfText

ENDIF

ENDWHILE

CLOSEFILE "FileA.txt"

CLOSEFILE "FileB.txt"

1
2
3
4
5

15

Fill in the Blanks

Create a text file.

DECLARE myTextFile : STRING

← "myText.txt"

16

Reorder

Reorder the following

OUTPUT "The file contains these lines of text:"

REPEAT

OPEN myTextFile FOR READ

    READFILE, textIn

    OUTPUT textIn

UNTIL EOF(myTextFile)

CLOSEFILE(myTextFile)

1
2
3
4
5

17

Reorder

Append a line of text at the end of the file.

DECLARE textIn : STRING

REPEAT

   

       

    ELSE

   

OPEN myTextFile FOR APPEND

    OUTPUT "Please enter a line of text"

    INPUT textIn

 IF textIn <> "" THEN

 WRITEFILE, textIn

     CLOSEFILE(myTextFile)

    ENDIF

UNTIL textIn = ""

1
2
3
4
5
pattern-tertiary
File

By Arasaka Teacher

Show answer

Auto Play

Slide 1 / 17

SLIDE