Search Header Logo
Using Text Files

Using Text Files

Assessment

Presentation

•

Computers

•

9th - 12th Grade

•

Practice Problem

•

Easy

Created by

Katherine Valenti

Used 1+ times

FREE Resource

86 Slides • 5 Questions

1

Using Text Files

2

Students will be able to...

  • Read data from external text files using Java’s File and Scanner classes

  • Store and analyze file data using arrays and string methods

  • Use try-catch blocks to handle IOException errors gracefully

  • Parse data using split() and apply algorithms to extract insights

  • Read, convert, and analyze numerical data from files

3

Warm Up

4

Open Ended

Why might a program need to read information from a file instead of asking the user to type everything

5

Warm Up

  • Handle larger data

  • Save time

  • Be reused across sessions

6

Open Ended

What do you expect could go wrong when trying to read a file?

7

Warm Up

  • File might not exist

  • Might be in the wrong place

  • Might have formatting issues

8

media

9

media

10

media

11

media

12

media

13

media

14

media

15

media

16

media

17

media

18

media

19

media

20

media

21

media

22

media

23

media

24

media

25

media

26

media

27

media

28

media

29

media

30

media

31

media

32

media

33

media

34

media

35

media

36

media

37

media

38

media

39

media

40

media

41

media

42

media

43

media

44

media

45

media

46

media

47

media

48

media

49

media

50

media

51

media

52

media

53

media

54

Try-Catch with IO Exception

  • Another way to terminate if an error occurs is to use a try-catch block

    • More graceful

    • Display a more informative message

media

55

Try-Catch with IO Exception

  • Try Block: write the code to access and manipulate the file data

  • Catch Block: explains what to do if a specific error occurs

media

56

Try-Catch with IO Exception

  • Catch condition: in parentheses, specify the type of error you want to catch

    • Files- IOException error with variable e, which contains info about the exception

media

57

Try-Catch with IO Exception

  • e.getMessage()- returns a human-readable description of the error

    • Can use in a print statement to inform the user of the error

media

58

Now It's Your Turn

  • Head to CodeHS and access the Using Text Files modules. You will be completing:

    • Read in a Haiku

    • Karaoke

    • Exploration: Reading Files and Arrays

    • Best Men's Soccer Players: Reading Files and Arrays

    • Exploration: Split Method

59

media

60

media

61

media

62

media

63

media

64

media

65

media

66

media

67

media

68

media

69

media

70

media

71

media

72

media

73

media

74

media

75

media

76

media

77

media

78

media

79

media

80

media

81

media

82

media

83

media

84

media

85

Now It's Your Turn!

  • Head to CodeHS and access the Using Text Files (Day 2) modules. You will be completing:

    • Exploration: Number of Active Days

    • FitBit: Read in Sleep Data

    • FitBit: Analyze Sleep Data

86

Wrap Up

87

Match

Match the following concepts:

Process of reading information from and writing information to files

Error related to file access or reading

Java construct to gracefully handle runtime errors

Method that divides a string based on a delimeter

Extracting and converting data from Strings

File I/O (File Input/Output)

IOException

try-catch

Split Method

Parsing

88

Open Ended

What is the purpose of a try-catch block when working with files?

89

Wrap Up

  • Prevent the program from crashing

  • Provide error messages if the file can't be opened

90

Open Ended

How does using an array help when working with data from a file?

91

Wrap Up

  • Stores all entries for later use in one location

    • Helps in filtering, searching, or averaging

Using Text Files

Show answer

Auto Play

Slide 1 / 91

SLIDE