Filbehandling - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Filbehandling

Description:

public File(String pathname) Creates a new File instance Parameters: ... catch (IOException e){ Skriv_ut('Feil' e.getMessage()); Skrive til fil. Lese fra fil ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 10
Provided by: Ans124
Category:

less

Transcript and Presenter's Notes

Title: Filbehandling


1
Filbehandling
  • Tekstfiler
  • Notepad, TextPad
  • Word Save As - Tekstfil

2
Pakken java.io.
  • klassen File
  • File Management

3
Constructor Summary public File(String pathname)
  • Creates a new File instance Parameters
  • pathname - A pathname string
  • Throws
  • NullPointerException - If the pathname argument
    is null

4
Metoder
  • boolean canRead()
  • Tests whether the application can read the file
    denoted by this abstract pathname.
  • boolean canWrite()
  • Tests whether the application can modify to the
    file denoted by this abstract pathname.

5
Metoder
  • Boolean createNewFile()
  • Atomically creates a new, empty file named by
    this abstract pathname if and only if a file with
    this name does not yet exist.
  • Boolean delete()
  • Deletes the file or directory denoted by this
    abstract pathname.

6
Metoder
  • boolean exists()
  • Tests whether the file denoted by this abstract
    pathname exists.
  • boolean isDirectory()
  • boolean isFile()  
  • String list()
  • Returns an array of strings naming the files and
    directories in the directory denoted by this
    abstract pathname.
  • static File listRoots()
  • List the available filesystem roots.

7
Opprette fil
  • import java.io.
  • File fnew File("a.txt")
  • try
  • if (f.createNewFile())
  • Skriv_ut("Filen ble laget")
  • else
  • Skriv_ut("Filen ble ikke laget")
  • catch (IOException e)
  • Skriv_ut(Feil "e.getMessage())

8
Skrive til fil
  •  import java.io.
  • File fnew File("a.txt")
  • try
  • if (f.exists()) return
  • FileWriter fmiddlenew FileWriter(f)
  • PrintWriter foutnew PrintWriter(fmiddle)
  • fout.print("Testing")
    // som System.out.
  • fout.println("og ting")
  • fout.close()
  • catch (IOException e)
  • Skriv_ut(Feile.getMessage())

9
Lese fra fil
  • File fnew File("a.txt")
  • FileReader fmiddlenew FileReader(f)
  • BufferedReader innew BufferedReader(fmiddle)
  • sin.readLine() // eneste
  • in.close()
Write a Comment
User Comments (0)
About PowerShow.com