• Tidak ada hasil yang ditemukan

File

N/A
N/A
Protected

Academic year: 2024

Membagikan "File"

Copied!
11
0
0

Teks penuh

(1)

File

(2)

File inFile = new File(“input.txt”);

Scanner in = new Scanner(inFile);

แสกนเนอร์นี ้จะอ่าน ไฟล์จาก input.txt

ต่อจากนี ้ก็ใช้ next, nextLine,

nextInt ของ Scanner อ่าน ไฟล์ได้ตามใจ

อ่านไฟล์

(3)

เขียนไฟล์

PrintWriter out = new PrintWriter(“output.txt”);

ถ้ามีไฟล์อยู่แล้ว ไฟล์จะถูกล้างทิ ้ง แต่ถ้าไม่มี

ไฟล์ ไฟล์เปล่าๆจะถูกสร้างขึ้นให้เรา

ถ้ามี File object อยู่

แล้ว ก็ใช้ File object เป็นพารามิเตอร์ก็ได้

out.println(…..) ก็จะถูกเรียกใช้ได้

เวลาจะเลิกเขียนอะไรลงไฟล์แล้ว อย่าลืม out.close();

(4)

ตัวอย่างโปรแกรม

อ่านไฟล์หนึ่ง แล้วไปเขียนลงอีกไฟล์ โดยเติมหมายเลขบรรทัดลงไปด้าน หน้าข้อความทุกบรรทัด

I am Sam.

I am a man.

/* 1 */ I am Sam.

/* 2 */ I am a man.

(5)

import java.io.*;

import java.util.*;

public class LineNumberer {

public static void main(String[] args) throws FileNotFoundException {

Scanner console = new Scanner(System.in);

System.out.print("Input File: ");

String inputFileName = console.next();

System.out.print("Output file: ");

String outputFileName = console.next();

(6)

File inputFile = new File(inputFileName);

Scanner in = new Scanner(inputFile); // May not find the specified file.

PrintWriter out = new PrintWriter(outputFileName);

int lineNumber = 1;

while (in.hasNextLine()) { String line = in.nextLine();

out.println("/* " + lineNumber + " */ " + line);

lineNumber++;

}

in.close();

out.close();

} }

จะมีประโยชน์ตอนใช้กับ in.nextDouble()

(7)

ซึ่งอ่านทีละบรรทัดเราจะสามารถเอาทั้งบรรทัดมาใช้ต่อได้

เช่น

int i=0;

While (!Character.isDigit(line.charAt(i))){

i++;

}

เช่น ใช้กับไฟล์ที่บนหนึ่งบรรทัด มี ชื่อประเทศ ตามด้วยตัวเลขแสดงจ านวนประชากร String countryName = line.substring(0,i);

String population = substring(i);

int populationValue = Integer.parseInt(population.trim());

(8)

Reading one word at a time

while(in.hasNext()){

String input = in.next();

System.out.println(input);

}

อ่านทีละค า แต่ค าๆหนึ่งคั่นด้วย เว้นวรรค

tab หรือ new line เท่านั้น ดังนั้น

Snow.

22234 C++

ก็ถือเป็นหนึ่งค าเช่นกัน

(9)

ถ้าจะเอาส่วนที่ไม่ใช่ค า ทิ้งไปล่ะ

Scanner in = new Scanner(…);

In.useDelimiter(“*^A-Za-z++”);

(10)

นิดหน่อย กับ nextDouble

• มันไม่เอา white space ข้างหลังออกให้นะ

• สมมิติเรียก nextInt() กับ 1729\nHarry

จะเหลือ \nHarry

(11)

Reading 1 character at a time

Scanner in = new Scanner(…);

In.useDelimiter(“”);

while (in.hasNext()){

char ch = in.next().charAt(0);

// คราวนี้ก็เล่นกับ ch ได้ตามใจ

}

บังคับตรงนี้นั่นเอง

Referensi

Dokumen terkait

“Hello World!” in Java COMPSCI 230: OOD 11  HelloWorld.javaJava source code: public class HelloWorld { public static void mainString[] args { System.out.println"Hello World!"; } }

“Hello World!” in Java  HelloWorld.java Java source code: public class HelloWorld { public static void mainString[] args { System.out.println"Hello World!"; } }  This is the

Solution In one of your classes, ensure you implement astaticmethod named Mainwith one of the following signatures: public static void Main; public static void Mainstring[] args;

// // class TestPC.java // import person.*; class TestPC { public static void mainString args[] { // Initialize the eyedb package and parse the default eyedb options // on the

void bark{System.out.println"barking...";} } class TestInheritance{ public static void mainString args[]{ Dog d=new Dog; d.bark; d.eat; }} Polymorphism in Java

Example import javax.swing.*; import java.awt.*; public class OnePrgLine extends JPanel{ public static void mainString[] args { int x1,y1,xn,yn; JFrame frame = new

import java.util.Scanner; public class AccountTest { // main method begins execution of Java application public static void main String args[] { Account account1 = new Account

TOUIR Page 3 // import Section – import used java libraries public class MyProgramName { // main method public static void main String args[] { // Declaration section – Declare