java/¥Õ¥¡¥¤¥ëÆþ½ÐÎÏ
Last-modified: 2018-11-14 (¿å) 15:24:10 (2579d)
java ¤½¤ì¤¾¤ì¡¢¼¡¤Îimport¤¬É¬ÍפǤ¹¡£
import java.io.*;
¥Æ¥¥¹¥È¥Õ¥¡¥¤¥ëÆÉ¤ß¹þ¤ß †
FileReader fr;
try {
fr = new FileReader("¥Õ¥¡¥¤¥ë̾");
int c;
String str = new String();
while ((c = fr.read()) != -1) {
str = str + (char)c;
}
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (fr != null) {
fr.close();
fr = null;
}
} catch (IOException e2) {
}
}
¥Æ¥¥¹¥È¥Õ¥¡¥¤¥ë½ñ¤¹þ¤ß †
FileWriter fw;
try {
fw = new FileWriter("¥Õ¥¡¥¤¥ë̾");
fw.write("½ñ¤¹þ¤ß¤¿¤¤Ê¸»úÎó");
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (fw != null) {
fw.close();
fw = null;
}
} catch (IOException e2) {
}
}
¥Ð¥Ã¥Õ¥¡ÉÕ¤¥Æ¥¥¹¥È¥Õ¥¡¥¤¥ëÆÉ¤ß¹þ¤ß †
BufferedReader br;
try {
br = new BufferedReader(new FileReader("¥Õ¥¡¥¤¥ë̾"));
String str = new String();
while ((str = br.readLine()) != null) {
System.out.println(str); // ½èÍý¤òµ½Ò
}
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (br != null) {
br.close();
br = null;
}
} catch (IOException e2) {
}
}
¥Ð¥Ã¥Õ¥¡ÉÕ¤¥Æ¥¥¹¥È¥Õ¥¡¥¤¥ë½ñ¤¹þ¤ß †
BufferedWriter bw;
try {
bw = new BufferedWriter(new FileWriter("¥Õ¥¡¥¤¥ë̾"));
bw.write(½ñ¤¹þ¤ß¤¿¤¤¥Ç¡¼¥¿);
bw.newLine; // ¹Ô¶èÀÚ¤ê
bw.flush();
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (bw != null) {
bw.close();
bw = null;
}
} catch (IOException e2) {
}
}
¥Ð¥¤¥Ê¥ê¥Õ¥¡¥¤¥ë byteñ°Ì¤ÎÆÉ¤ß¹þ¤ß †
FileInputStream fis;
try {
fis = new FileInputStream("¥Õ¥¡¥¤¥ë̾");
int b;
while ((b = fis.read()) != -1) {
System.out.println(b); // ½èÍý¤òµ½Ò
}
} catch (FileNotFoundException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (fis != null) {
fis.close();
fis = null;
}
} catch (IOException e2) {
}
}
¥Ð¥¤¥Ê¥ê¥Õ¥¡¥¤¥ë byteñ°Ì¤Î½ñ¤¹þ¤ß †
FileOutputStream fos;
try {
fos = new FileOutputStream("¥Õ¥¡¥¤¥ë̾");
fos.write(½ñ¤¹þ¤ß¤¿¤¤¥Ç¡¼¥¿);
} catch (FileNotFoundException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (fos != null) {
fos.close();
fos = null;
}
} catch (IOException e2) {
}
}
¥Ð¥¤¥Ê¥ê¥Õ¥¡¥¤¥ë³Æ¼ï¥Ç¡¼¥¿ÆÉ¤ß¹þ¤ß †
DataInputStream dis;
try {
dis = new DataInputStream(new BufferedInputStream(new FileInputStream("¥Õ¥¡¥¤¥ë̾")));
// dis = new DataInputStream(new FileInputStream("¥Õ¥¡¥¤¥ë̾")); ¥Ð¥Ã¥Õ¥¡Ìµ¤·¤Î¾ì¹ç
int i = dis.readInt();
String str = dis.readUTF();
} catch (FileNotFoundException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (dis != null) {
dis.close();
dis = null;
}
} catch (IOException e2) {
}
}
¥Ð¥¤¥Ê¥ê¥Õ¥¡¥¤¥ë³Æ¼ï¥Ç¡¼¥¿½ñ¤¹þ¤ß †
DataOutputStream dos;
try {
dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("¥Õ¥¡¥¤¥ë̾")));
// dos = new DataOutputStream(new FileOutputStream("¥Õ¥¡¥¤¥ë̾")); ¥Ð¥Ã¥Õ¥¡Ìµ¤·¤Î¾ì¹ç
dos.writeInt(½ñ¤¹þ¤ß¤¿¤¤int·¿¥Ç¡¼¥¿);
dos.writeUTF(½ñ¤¹þ¤ß¤¿¤¤Ê¸»úÎó¥Ç¡¼¥¿);
} catch (FileNotFoundException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
} finally {
try {
if (dos != null) {
dos.close();
dos = null;
}
} catch (IOException e2) {
}
}