黒馬プログラマーのJAVAIO(二)
10228 ワード
-----Javaトレーニング、Androidトレーニング、iOSトレーニング、Netトレーニング、あなたと交流することを期待します!------
一、File類
例1:
例2:
例3:
二、ObjectOutputStreamとObjectInputStream
例:
三、PipedInputStreamとPipedOutputStream
例:
四、PrintStreamとPrintWriter
例:
五、Properties
例:
一、File類
例1:
import java.io.File;
import java.io.IOException;
/*
* File
* 1.
* boolean createNewFile() // , , ( false)
* , , ,
* boolean mkdir() // , ( false)
* boolean mkdirs() //
*
* 2.
* boolean delete() // , false
* void deleteOnExit() // JVM
*
* 3.
* boolean exists() //
* boolean isDirectory() //
* boolean isFile() //
* boolean isHidden() //
* boolean isAbsolute() // , "D:/a/a/a.txt",
* // true, false, , File
*
* 4.
* String getAbsolutePath() //
* String getPath() // ( )
* String getName() //
* String getParent() // , null, :"a.txt", null
* // , , : "b/a.txt", b
* long lastModified() //
* boolean renameTo() // ( , , )
*
*
*/
public class FileDemo {
public static void main(String[] args) throws IOException {
fileMethod4();
}
public static void cos() {
// d.txt
File file = new File("d.txt");
File file2 = new File("D:/", "a.txt");
File d = new File("D:/");
File file3 = new File(d, "e.txt");
// separator
File file4 = new File("D:" + File.separator + "d.txt");
System.out.println(file);
System.out.println(file2);
System.out.println(file3);
System.out.println(file4);
}
public static void fileMethod() throws IOException {
File file = new File("D:" + File.separator + "file.txt");
System.out.println("create : " + file.createNewFile());
boolean b = file.delete(); //
//
System.out.println("exists : " + file.exists());
File file2 = new File("D:" + File.separator + "a");
System.out.println("mkdir : " + file2.mkdir());
File file3 = new File("D:" + File.separator + "a" + File.separator + "b");
System.out.println("mkdirs : " + file3.mkdirs());
}
public static void fileMethod2(){
// ,
// exists()
File file = new File("D:" + File.separator + "fos.txt");
boolean b = file.isFile();
boolean d = file.isDirectory();
System.out.println("isFile : " + b);
System.out.println("isDirectory : " + d);
boolean p = file.isAbsolute(); //
}
public static void fileMethod3(){
File file = new File("D:/3.txt");
//
String path = file.getAbsolutePath();
// ( )
String path2 = file.getPath();
//
String name = file.getName();
//
String path3 = file.getParent();
//
long lastTime = file.lastModified();
//
long size = file.length();
System.out.println("getAbsolutePath : " + path);
System.out.println("getPath : " + path2);
System.out.println("name : " + name);
System.out.println("getParent : "+ path3);
System.out.println("lastModified : " + lastTime);
System.out.println("length : "+ size);
}
public static void fileMethod4(){
File file = new File("D:/q.txt");
File newFile = new File("D:/1.txt");
// ( , )
boolean b = file.renameTo(newFile);
System.out.println(b);
}
}
例2:
public static void listDemo(){
File file = new File("C:/");
// ( )
// list() File ,
String[] strings = file.list();
for (String string : strings) {
System.out.println(string);
}
}
public static void acceptFile(){
File dir = new File("D:/");
//
String[] files = dir.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".mp3");
}
});
for (String string : files) {
System.out.println(string);
}
}
例3:
import java.io.File;
// ,
// , .
// ,
// , , .
// :
//1. ( )
//2. ,
public class FileDemo3 {
public static void main(String[] args) {
showDir(new File("D:/"));
}
public static void showDir(File dir) {
System.out.println(dir);
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
// , ,
if (files[i].isDirectory() && !files[i].isHidden())
showDir(files[i]);
else
System.out.println(files[i]);
}
}
}
二、ObjectOutputStreamとObjectInputStream
例:
//
// ,
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ObjectStreamDemo01 {
public static void main(String[] args) throws Exception {
readObj();
}
public static void writeObj() throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/p.txt"));
oos.writeObject(new Person("tom", 2222));
oos.flush();
oos.close();
}
public static void readObj() throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/p.txt"));
Person p = (Person)ois.readObject();
ois.close();
System.out.println(p);
}
}
//Serializable , Serializable
@SuppressWarnings("serial")
class Person implements Serializable{
private String name;
//transient ,
private transient int age;
//
private static String cn;
public Person(String name, int age) {
this.age = age;
this.name = name;
}
public String toString(){
return name + " : " + age;
}
}
三、PipedInputStreamとPipedOutputStream
例:
//
// ; 。
// , PipedInputStream , PipedOutputStream。
// , 。
// , 。
// , 。
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedStreamDemo02 {
public static void main(String[] args) throws IOException {
// 、
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
Read r = new Read(in);
Write w = new Write(out);
new Thread(r).start();
new Thread(w).start();
}
}
class Read implements Runnable{
private PipedInputStream in;
public Read(PipedInputStream in) {
this.in = in;
}
@Override
public void run() {
try {
byte[] b = new byte[1024];
int len = in.read(b);
String str = new String(b, 0, len);
System.out.println(str);
} catch (Exception e) {
throw new RuntimeException(" ");
}
}
}
class Write implements Runnable{
private PipedOutputStream out;
public Write(PipedOutputStream out) {
this.out = out;
}
@Override
public void run() {
try {
out.write("nadadadafafaffwgfwgs".getBytes());
out.close();
} catch (Exception e) {
throw new RuntimeException(" ");
}
}
}
四、PrintStreamとPrintWriter
例:
/*
* :
* ,
*
* :
* PrintStream
* :
* 1.file . File
* 2. . String
* 3. . OutputStream
*
*
* PrintWriter
* :
* 1.file . File
* 2. . String
* 3. . OutputStream
* 4. . Writer
*
*
*/
public class PrintStreamDemo {
public static void main(String[] args) throws IOException {
BufferedReader bufr =
new BufferedReader(new InputStreamReader(System.in));
// : true, , println、printf format
//new FileWriter("D:/printWriter.txt")
// out.flush()
//PrintWriter out = new PrintWriter(new FileWriter("D:/printWriter.txt"),true);
PrintWriter out = new PrintWriter(System.out);
String line = null;
while(null != (line=bufr.readLine())){
if("over".equals(line)) break;
//
out.println(line);
out.flush();
}
bufr.close();
out.close();
}
}
五、Properties
例:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
/*
* Properties Hashtable
* map ,
* IO
*
* :
*
*/
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
loadDemo();
}
public static void setAndGet(){
Properties prop = new Properties();
//
prop.setProperty("tom", " ");
prop.setProperty("Jerry", " ");
//
String value = prop.getProperty("tom");
// System.out.println(value);
// Set
Set key = prop.stringPropertyNames();
for (String string : key) {
System.out.println(string + " -> " + prop.getProperty(string));
}
}
//
public static void loadDemo() throws IOException{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("D:/1.txt");
//
prop.load(fis);
prop.setProperty("qeqeq", "33");
FileOutputStream os = new FileOutputStream("D:/1.txt");
// ,
prop.store(os, null);
// System.out.println(prop);
//
prop.list(System.out);
os.close();
fis.close();
}
}