package javase.day18;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo {
public static void main(String[] args) {
FileWriter fw=null;
try {
fw = new FileWriter("C:\java_test\FileWriterTest.txt");
//调用write 方法,将字符串写入到流中
fw.write("alex test23");
//刷新流对象中的缓冲中的数据
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(fw!=null){
//关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据
fw.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
package javase.day18;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo {
public static void main(String[] args) {
FileWriter fw=null;
try {
fw = new FileWriter("C:\java_test\FileWriterTest.txt");
//调用write 方法,将字符串写入到流中
fw.write("alex test23");
//刷新流对象中的缓冲中的数据
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(fw!=null){
//关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据
fw.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
package javase.day18;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
try {
FileReader fr=new FileReader("C:\java_test\SystemDemo.java");
char[] buf=new char[1024];
int num=0;
while((num=fr.read(buf))!=-1){
System.out.println(new String(buf,0,num));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package javase.day18;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
try {
FileReader fr=new FileReader("C:\java_test\SystemDemo.java");
char[] buf=new char[1024];
int num=0;
while((num=fr.read(buf))!=-1){
System.out.println(new String(buf,0,num));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package javase.day18;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyText {
public static void main(String[] args) {
try {
copy_1();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy_1() throws IOException{
FileWriter fw = new FileWriter("C:\java_test\Copy_SystemDemo.java");
FileReader fr = new FileReader("C:\java_test\SystemDemo.java");
int num=0;
while((num=fr.read())!=-1){
fw.write(num);
}
fw.close();
fr.close();
}
public static void copy_2() throws IOException{
FileWriter fw = new FileWriter("C:\java_test\Copy_SystemDemo.java");
FileReader fr = new FileReader("C:\java_test\SystemDemo.java");
int num=0;
char[] buf=new char[1024];
while((num=fr.read(buf))!=-1){
fw.write(buf,0,num);
}
fw.close();
fr.close();
}
}
package javase.day18;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyText {
public static void main(String[] args) {
try {
copy_1();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void copy_1() throws IOException{
FileWriter fw = new FileWriter("C:\java_test\Copy_SystemDemo.java");
FileReader fr = new FileReader("C:\java_test\SystemDemo.java");
int num=0;
while((num=fr.read())!=-1){
fw.write(num);
}
fw.close();
fr.close();
}
public static void copy_2() throws IOException{
FileWriter fw = new FileWriter("C:\java_test\Copy_SystemDemo.java");
FileReader fr = new FileReader("C:\java_test\SystemDemo.java");
int num=0;
char[] buf=new char[1024];
while((num=fr.read(buf))!=-1){
fw.write(buf,0,num);
}
fw.close();
fr.close();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class IOUtil {
/**
* 将文件读入到一个String,利用FileReader+BufferedReader(提供readLine方法)
*
* @param fileName
* @return String
*/
public static String fileReaderStringHandle(String fileName) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader(new File(
fileName).getAbsoluteFile()));
try {
String s;
while ((s = in.readLine()) != null) {
sb.append(s);
sb.append("n");
}
} finally {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return sb.toString();
}
/**
* 使用FileInputStream+BufferedInputStream以byte的方式处理文件
*
* @param fileName
* @return byte[]
*/
public static byte[] fileReaderByteHandle(String fileName) {
byte[] data = null;
try {
BufferedInputStream bf = new BufferedInputStream(
new FileInputStream(fileName));
try {
data = new byte[bf.available()];
bf.read(data);
} finally {
bf.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return data == null ? new byte[] {} : data;
}
/**
* 将指定的text写入到文件名为fileName的文件中
*
* @param fileName
* @param text
*/
public static void fileWriterHandle(String fileName, String text) {
try {
PrintWriter out = new PrintWriter(new File(fileName)
.getAbsoluteFile());
try {
out.print(text);
} finally {
out.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws IOException {
System.out.print(fileReaderStringHandle("src/IOUtil.java"));
for (byte b : fileReaderByteHandle("src/IOUtil.java"))
System.out.print(b);
fileWriterHandle("zj.txt",
fileReaderStringHandle("src/IOUtil.java"));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有