文件的复制:
需求:将C盘下的一个文本文件复制到D盘下。(其实就是将C盘下的文件数据存储到D盘下的一个文件中)
步骤:
1.在D盘中创建一个文件,用于存储C盘文件中的数据。
2.定义读取流和C盘文件关联
3.通过不断的读写完成数据存储。
4.关闭资源。
通过缓冲区复制一个.java文件
读取键盘录入:
System.out:对应的是标准输出设备:控制台
System.in:对应的是标准输入设备:键盘。
需求:
通过键盘录入数据,当录入一行数据后,就将该行数据进行打印,如果录入的数据是over,那么就停止录入
示例代码:
改变标准输入输出设备:
将异常信息保存到硬盘文件中:
将系统属性信息保存到硬盘文件中:
参考: http://blog.csdn.net/chengjun77/article/details/23040137
需求:将C盘下的一个文本文件复制到D盘下。(其实就是将C盘下的文件数据存储到D盘下的一个文件中)
步骤:
1.在D盘中创建一个文件,用于存储C盘文件中的数据。
2.定义读取流和C盘文件关联
3.通过不断的读写完成数据存储。
4.关闭资源。
示例代码:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
class CopyText {
public static void main(String[] args) throws IOException {
copy();
}
public static void copy() {
FileReader fr = null;
FileWriter fw = null;
try { // 创建文件输入输出流对象
fr = new FileReader("c:\\demo.txt");
fw = new FileWriter("d:\\demoD.txt", true);
// 定义一个临时数组
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
fw.write(buf, 0, len);
}
} catch (IOException e) {
throw new RuntimeException("读写失败");
} finally {
if (fr != null)
try {
// 关闭流
fr.close();
} catch (IOException e) {
System.out.println(e.toString());
}
if (fw != null)
try {
// 关闭流
fw.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
}
通过缓冲区复制一个.java文件
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyTextByBuf {
public static void main(String[] args) {
BufferedWriter bufw = null;
BufferedReader bufr = null;
try {
bufw = new BufferedWriter(new FileWriter("e:\\bufD.txt", true));
bufr = new BufferedReader(new FileReader("c:\\buf.txt"));
String line = null;// 相当于中转站。
while ((line = bufr.readLine()) != null) {
bufw.write(line);
bufw.newLine();
bufw.flush();
}
} catch (IOException e) {
throw new RuntimeException("读写失败");
} finally {
if (bufw != null)
try {
bufw.close();
} catch (IOException e) {
throw new RuntimeException("写入关闭失败");
}
if (bufr != null)
try {
bufr.close();
} catch (IOException e) {
throw new RuntimeException("读取关闭失败");
}
}
}
}
读取键盘录入:
System.out:对应的是标准输出设备:控制台
System.in:对应的是标准输入设备:键盘。
需求:
通过键盘录入数据,当录入一行数据后,就将该行数据进行打印,如果录入的数据是over,那么就停止录入
示例代码:
import java.io.IOException;
import java.io.InputStream;
public class ReadIn {
public static void main(String[] args) throws IOException {
// 获取输入流对象
InputStream in = System.in;
StringBuilder sb = new StringBuilder();
while (true) {
int ch = in.read();
if (ch == '\r') // 判断回车符
continue;
if (ch == '\n') // 判断回车符
{
String s = sb.toString();
if ("over".equals(s))
break;
System.out.println(s.toUpperCase());// 将键盘录入的字符以大写形式输出
sb.delete(0, sb.length());
} else
sb.append((char) ch);
}
}
}
改变标准输入输出设备:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
public class TransStreamDemo2 {
public static void main(String[] args) throws IOException {
// 改变标准输入输出设备
System.setIn(new FileInputStream("ReadIn.java"));
System.setOut(new PrintStream("123.txt"));
// 键盘的最常见写法。
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));// 只需改变源就可以实现源的不同来源
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(
System.out));// 只需改变目的,就可以实现目的不同展现
String line = null;
while ((line = bufr.readLine()) != null) {
if ("over".equals(line))
break;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
bufr.close();
}
}
将异常信息保存到硬盘文件中:
import java.io.IOException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExceptionInfo {
public static void main(String[] args) throws IOException {
try {
int[] arr = new int[2];
System.out.println(arr[3]);
} catch (Exception e) {
try {
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String s = sdf.format(d);
PrintStream ps = new PrintStream("exception.log");
ps.println(s);
System.setOut(ps);
} catch (IOException ex) {
throw new RuntimeException("日志文件创建失败");
}
e.printStackTrace(System.out);
}
}
}
将系统属性信息保存到硬盘文件中:
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
public class SystemInfo {
public static void main(String[] args) throws IOException {
Properties prop = System.getProperties();
prop.list(new PrintStream("systeminfo.txt"));
}
}
参考: http://blog.csdn.net/chengjun77/article/details/23040137