程序与文件|数组|网络文件|数据库
package com.zixin.learn;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Demo01 {
public static void main(String[] args) {
// 建立联系
File f1 = new File("d://1.sql");
// 选择流
InputStream in = null;
try {
in = new FileInputStream(f1);
// 操作不断读取缓冲数组
byte[] b = new byte[10];
int len = 0;// 接收 实际读取大小
while (-1 != (len = in.read(b))) {
// 输出字节数组
String info = new String(b, 0, len);
System.out.println(info);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.zixin.learn;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Demo02 {
public static void main(String[] args) {
File file = new File("d://te.txt");
OutputStream os = null;
try {
//追加形式写入文件
os = new FileOutputStream(file, true);
String s = "zixin";
byte[] data = s.getBytes();
os.write(data, 0, data.length);
os.flush();// 强制刷新出去
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
} finally {
if (null != os) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package com.zixin.learn;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class Demo03 {
public static void main(String[] args) throws Exception {
// 建立联系
File src = new File("d://te.txt");
File dest = new File("d://te1.txt");
// 选择流
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
// 文件拷贝
byte[] flush = new byte[1024];
int len = 0;
// 读取
while (-1 != (len = in.read(flush))) {
// 写出
out.write(flush, 0, len);
}
out.flush();// 强制输出
// 先打开的后关闭
out.close();
in.close();
}
}
public static void copyDirDetail(File src, File dest) throws Exception {
if (src.isFile()) {
// 选择流
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
// 文件拷贝
byte[] flush = new byte[1024];
int len = 0;
// 读取
while (-1 != (len = in.read(flush))) {
// 写出
out.write(flush, 0, len);
}
out.flush();// 强制输出
// 先打开的后关闭
out.close();
in.close();
} else if (src.isDirectory()) {
dest.mkdirs();
// 获取下一级目录|文件
for (File sub : src.listFiles()) {
copyDirDetail(sub, new File(dest, sub.getName()));
}
}
}
package com.zixin.learn;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo05 {
public static void main(String[] args) {
File file = new File("d://te.txt");
FileReader reader = null;
try {
reader = new FileReader(file);
char[] flush = new char[10];
int len = 0;
while (-1 != (len = reader.read(flush))) {
String str = new String(flush, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package com.zixin.learn;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class Demo06 {
public static void main(String[] args) {
File file = new File("d://te2.txt");
Writer wr = null;
try {
// 选择流 true表示追加文件
wr = new FileWriter(file,true);
String ms = "";
wr.write(ms);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != wr) {
try {
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.zixin.learn;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class Demo07 {
public static void main(String[] args) {
File src = new File("d://1.txt");
File dest = new File("d://2.txt");
Reader reader = null;
Writer writer = null;
try {
reader = new FileReader(src);
writer = new FileWriter(dest);
char[] flush = new char[10];
int len = 0;
while (-1 != (len = reader.read(flush))) {
writer.write(flush, 0, len);
}
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(null!=writer) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=reader) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.zixin.learn;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class Demo08 {
public static void main(String[] args) throws Exception {
// 建立联系
File src = new File("d://te.txt");
File dest = new File("d://te1.txt");
// 选择流
InputStream in = new BufferedInputStream(new FileInputStream(src));
OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
// 文件拷贝
byte[] flush = new byte[1024];
int len = 0;
// 读取
while (-1 != (len = in.read(flush))) {
// 写出
out.write(flush, 0, len);
}
out.flush();// 强制输出
// 先打开的后关闭
out.close();
in.close();
}
}
package com.zixin.learn;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo09 {
public static void main(String[] args) {
File src = new File("d://1.txt");
File dest = new File("d://2.txt");
BufferedReader reader = null;
BufferedWriter writer = null;
try {
reader = new BufferedReader(new FileReader(src));
writer = new BufferedWriter(new FileWriter(dest));
String line = null;
while (null != (line = reader.readLine())) {
writer.write(line);
writer.append("\n");
writer.newLine();
}
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}