国外java代码例子经典 国外java英文视频教程(12)


System.in));
System.out.print("Enter a line:");
System.out.println(stdin.readLine());
// 2. 从内存中读入
StringReader in2 = new StringReader(s2);
int c;
while ((c = in2.read()) != -1) {
System.out.print((char) c);
}
// 3. 格式化内存输入
try {
DataInputStream in3 = new DataInputStream(new ByteArrayInputStream(
s2.getBytes()));
while (true) {
System.out.print((char) in3.readByte());
}
} catch (EOFException e) {
System.err.println("End of stream");
}
// 4. 文件输入
try {
BufferedReader in4 = new BufferedReader(new StringReader(s2));
PrintWriter out1 = new PrintWriter(new BufferedWriter(
new FileWriter("IODemo.out")));
int lineCount = 1;
while ((s = in4.readLine()) != null) {
out1.println(lineCount++ + ": " + s);
}
out1.close();
} catch (EOFException e) {
System.err.println("End of stream");
}
// 5. 接收和保存数据
try {
DataOutputStream out2 = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream("Data.txt")));
out2.writeDouble(3.14159);
out2.writeUTF("That was pi");
out2.writeDouble(1.41413);
out2.writeUTF("Square root of 2");
out2.close();
DataInputStream in5 = new DataInputStream(new BufferedInputStream(
new FileInputStream("Data.txt")));
System.out.println(in5.readDouble());
System.out.println(in5.readUTF());
System.out.println(in5.readDouble());
System.out.println(in5.readUTF());
} catch (EOFException e) {
throw new RuntimeException(e);
}
// 6. 随机读取文件内容
RandomAccessFile rf = new RandomAccessFile("rtest.dat", "rw");
for (int i = 0; i10; i++) {
rf.writeDouble(i * 1.414);
}
rf.close();
rf = new RandomAccessFile("rtest.dat", "rw");
rf.seek(5 * 8);
rf.writeDouble(47.0001);
rf.close();
rf = new RandomAccessFile("rtest.dat", "r");
for (int i = 0; i10; i++) {
System.out.println("Value " + i + ": " + rf.readDouble());
}
rf.close();
}
}
package IO;
import java.io.*;
/**
* p
* Title: JAVA进阶诀窍
* /p
*
* @author 张峰
* @version 1.0
*/
public class MakeDirectoriesExample {
private static void fileattrib(File f) {
System.out.println("绝对路径: " + f.getAbsolutePath() + "\n 可读属性: "
+ f.canRead() + "\n 可定属性: " + f.canWrite() + "\n 文件名: "
+ f.getName() + "\n 父目录: " + f.getParent() + "\n 当前路径: "
+ f.getPath() + "\n 文件长度: " + f.length() + "\n 最后更新日期: "
+ f.lastModified());
if (f.isFile()) {
System.out.println("输入的是一个文件");
} else if (f.isDirectory()) {
System.out.println("输入的是一个目录");
}
}
public static void main(String[] args) {
if (args.length1) {
args = new String[3];
}
args[0] = "d";
args[1] = "test1.txt";
args[2] = "test2.txt";
File old = new File(args[1]), rname = new File(args[2]);
old.renameTo(rname);
fileattrib(old);
fileattrib(rname);
int count = 0;
boolean del = false;
if (args[0].equals("d")) {
count++;
del = true;
}
count--;
while (++countargs.length) {
File f = new File(args[count]);
if (f.exists()) {
System.out.println(f + " 文件己经存在");
if (del) {
System.out.println("删除文件" + f);
f.delete();
}
} else { // 如果文件不存在
if (!del) {
f.mkdirs();
System.out.println("创建文件: " + f);
}
}
fileattrib(f);
}
}
}
java线程经典代码package threadgroup;
class ThreadDemo3 extends Thread {

推荐阅读