JAVA中利用输入输出流,将一个记事本中的内容写入另一个记事本中。去实现该功能的代码
3个回答
展开全部
//把file1的内容复制到file2的里面
import java.io.*;
public class FileDemo {
public static void main(String[] args){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("file1.txt");
fos = new FileOutputStream("file2.txt");
int ch = 0;
while( (ch = fis.read()) != -1){
fos.write(ch);
}
System.out.println("复制成功");
}catch(FileNotFoundException e){
System.out.println("NOT FOUND file1 or file2");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
fos.close();
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
希望帮到你!!!请自己测试一下。。。。
import java.io.*;
public class FileDemo {
public static void main(String[] args){
FileInputStream fis = null;
FileOutputStream fos = null;
try{
fis = new FileInputStream("file1.txt");
fos = new FileOutputStream("file2.txt");
int ch = 0;
while( (ch = fis.read()) != -1){
fos.write(ch);
}
System.out.println("复制成功");
}catch(FileNotFoundException e){
System.out.println("NOT FOUND file1 or file2");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
fos.close();
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
希望帮到你!!!请自己测试一下。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class fileOprate {
public static void main(String[] args) {
writeFile(readFile("D:/1.txt"),"D:/2.txt");
}
public static byte[] readFile(String filePath) {
return readFile(new File(filePath));
}
/**
* 读取出某个文件的内容
* @param file 需要读取的文件
* @return 文件内容
*/
public static byte[] readFile(File file) {
if(!file.exists())
return null;
FileInputStream fis = null;
int size = (int) file.length();
try {
fis = new FileInputStream(file);
byte buffer[] = new byte[size];
fis.read(buffer);
return buffer;
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
System.out.printf("File " + file.getAbsolutePath() + " Not Found", e);
return null;
} catch (IOException e) {
// TODO 自动生成 catch 块
System.out.printf("Read File " + file.getAbsolutePath() + " Error", e);
return null;
} finally {
if(fis!=null)
try {
fis.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
System.out.printf("Close File" + file.getAbsolutePath() + " Error", e);
return null;
}
}
}
/**
* 把字节数组写入某个文件
* @param data 需要写入的数据
* @param filePath 写入的文件
* @throws IOException 写入出现异常
*/
public static boolean writeFile(byte[] data, String filePath) {
File destFile = new File(filePath);
FileOutputStream fos = null;
try {
if (!destFile.getParentFile().exists())
if (!destFile.getParentFile().mkdirs())
System.out.println("Makedirs Error");
fos = new FileOutputStream(destFile);
fos.write(data);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
return false;
} finally {
if(fos!=null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return true;
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class fileOprate {
public static void main(String[] args) {
writeFile(readFile("D:/1.txt"),"D:/2.txt");
}
public static byte[] readFile(String filePath) {
return readFile(new File(filePath));
}
/**
* 读取出某个文件的内容
* @param file 需要读取的文件
* @return 文件内容
*/
public static byte[] readFile(File file) {
if(!file.exists())
return null;
FileInputStream fis = null;
int size = (int) file.length();
try {
fis = new FileInputStream(file);
byte buffer[] = new byte[size];
fis.read(buffer);
return buffer;
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
System.out.printf("File " + file.getAbsolutePath() + " Not Found", e);
return null;
} catch (IOException e) {
// TODO 自动生成 catch 块
System.out.printf("Read File " + file.getAbsolutePath() + " Error", e);
return null;
} finally {
if(fis!=null)
try {
fis.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
System.out.printf("Close File" + file.getAbsolutePath() + " Error", e);
return null;
}
}
}
/**
* 把字节数组写入某个文件
* @param data 需要写入的数据
* @param filePath 写入的文件
* @throws IOException 写入出现异常
*/
public static boolean writeFile(byte[] data, String filePath) {
File destFile = new File(filePath);
FileOutputStream fos = null;
try {
if (!destFile.getParentFile().exists())
if (!destFile.getParentFile().mkdirs())
System.out.println("Makedirs Error");
fos = new FileOutputStream(destFile);
fos.write(data);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
return false;
} finally {
if(fos!=null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return true;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package housework;
//自定义Employee对象,属性有:姓名,年龄,薪资等,
//用对象序列化把若干Employee对象写到文件中,再把文件内容读取出来,
//并将相关信息打印到控制台。
import java.io.*;
public class IO_4 {
public static void main(String[] args) throws Exception{
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("d:\\a.tmp"));
Employee1 e = new Employee1("tom",20,2000);
out.writeObject(e);
out.close();
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("d:\\a.tmp"));
Employee1 e1 = (Employee1) in.readObject();
System.out.println(e1.getName()+","+e1.getAge()+","+
e1.getSalary());
in.close();
}
}
class Employee1 implements Serializable {
private String name;
private int age;
private int salary;
public Employee1(String name,int age,int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
//自定义Employee对象,属性有:姓名,年龄,薪资等,
//用对象序列化把若干Employee对象写到文件中,再把文件内容读取出来,
//并将相关信息打印到控制台。
import java.io.*;
public class IO_4 {
public static void main(String[] args) throws Exception{
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("d:\\a.tmp"));
Employee1 e = new Employee1("tom",20,2000);
out.writeObject(e);
out.close();
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("d:\\a.tmp"));
Employee1 e1 = (Employee1) in.readObject();
System.out.println(e1.getName()+","+e1.getAge()+","+
e1.getSalary());
in.close();
}
}
class Employee1 implements Serializable {
private String name;
private int age;
private int salary;
public Employee1(String name,int age,int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询