import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class IOUtils {
public static void main(String[] args) {
File file = new File("C:\\SMS.txt");
File file2 = new File("C:\\SMS.txt");
String counte = "asdasdasdxzc\r\n";
// readFileByBufferedReader(file);
// readFileByStream(file);
// readFileByFileInputStream(file);
// writeFileByOutputStream(file2, counte);
writeByBufferedWriter(file2, counte);
}
/**
* 读取本地数据,打印到控制台。
* 1、以字符为单位,把数据从本地读取到内存中
* 2、将读取到内存中的数据,通过控制台打印出来,涉及到输出流。
* */
public static void readFileByBufferedReader(File file){
BufferedReader reader = null;
String tempString = null;
try {
reader = new BufferedReader(new FileReader(file));
try {
while((tempString = reader.readLine()) != null){
System.out.println(tempString);
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 字符流以字符为单位读取文件
*InputStreamReader
*
* FileInputStream
* */
public static void readFileByStream(File file){
Reader reader = null;
try {
reader = new InputStreamReader(new FileInputStream(file));
int tempChar;
try {
while((tempChar = reader.read()) != -1){
System.out.print((char)tempChar);
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 字节流,以字节为单位,读取文件
* FileInputStream
* */
public static void readFileByFileInputStream(File file){
InputStream inputStream = null;
byte[] tempChar = new byte[1024];
try {
int byteread;
inputStream = new FileInputStream(file);
while ((byteread = inputStream.read(tempChar)) != -1) {
String tempString = new String(tempChar, 0 ,tempChar.length);
System.out.println(tempString);
}
inputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 以文件字节流写入文件
*
* FileOutputStream
*/
public static void writeFileByOutputStream(File file, String count){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file, true);
byte[] countes = count.getBytes();
fileOutputStream.write(countes);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 以字符流的方式写入文件
*/
public static void writeByBufferedWriter(File file, String counte){
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter(file, true));
bufferedWriter.write(counte);
bufferedWriter.flush();
bufferedWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 文件的复制
* 1、涉及到输入流
* 2、输出流
*/
public static void copyFile(File oldFile, File newFile){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
byte readbyte[] = new byte[1024];
long length = 0;
int curlength;
try {
fileInputStream = new FileInputStream(oldFile);
fileOutputStream = new FileOutputStream(newFile);
while ((curlength = fileInputStream.read(readbyte)) != -1) {
fileOutputStream.write(readbyte, 0, curlength);
length +=curlength;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO: handle exception
}
}
}
I/O(一)
最新推荐文章于 2024-05-13 13:06:01 发布