创建简单的I/O文件

让程序写入文件:

1、创建一个ofstream对象管理输出流;

2、将该对象与特定的文件关联起来;

3、以使用cout的方式使用该对象,唯一的区别是输出将进入文件,而不是屏幕。


让程序读取文件:

1、创建一个ifstream对象管理输入流

2、将该对象与特定的文件关联起来;

3、以使用cin的方式使用该对象。


#include<iostream>	
#include<fstream>
#include<string>

using std::cout;    using std::endl;  using std::cin;
using std::string;  
using std::ifstream; using std::ofstream;

int main()
{
	string filename;

	cout << "Enter name for new file: ";
	cin >> filename;

	ofstream fout(filename.c_str());

	fout << "For your eyes only!\n";
	cout << "Enter your secret number: ";
	float secret;
	cin >> secret;
	fout << "Your secret number is " << secret << endl;
	fout.close();

	ifstream fin(filename.c_str());
	cout << "Here are the contents of " << filename << endl;
	char ch;
	while (fin.get(ch))
	{
		cout << ch;
	}
	fin.close();

	system("pause");
	return 0;
}

#include<iostream>	
#include<fstream>
#include<string>

using std::cout;    using std::endl;  using std::cin;
using std::string;  
using std::ifstream; using std::ofstream;

int main()
{
	string filename;

	cout << "Enter name for new file: ";
	cin >> filename;

	ofstream fout;
	fout.open(filename);

	fout << "For your eyes only!\n";
	cout << "Enter your secret number: ";
	float secret;
	cin >> secret;
	fout << "Your secret number is " << secret << endl;
	fout.close();

	ifstream fin(filename.c_str());
	cout << "Here are the contents of " << filename << endl;
	char ch;
	while (fin.get(ch))
	{
		cout << ch;
	}
	fin.close();

	system("pause");
	return 0;
}

两种与文件相关联的方式:第一个用c_str()方法给ofstream和ifstream的构造函数提供C风格字符串参数

     第二个用open()方法

关闭文件连接:用close()方法显示关闭,如果没有,输入和输出流对象过期时会自动关闭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Java 中使用 I/O 流复制文件可以使用如下的代码: ``` import java.io.*; public class CopyFile { public static void main(String[] args) { // 要复制的文件的路径 String srcFile = "/path/to/src/file.txt"; // 复制后的文件的路径 String destFile = "/path/to/dest/file.txt"; try (InputStream in = new FileInputStream(srcFile); OutputStream out = new FileOutputStream(destFile)) { // 缓冲区大小 byte[] buffer = new byte[1024]; int length; // 循环读取文件内容并写入到新文件中 while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } } catch (IOException e) { e.printStackTrace(); } } } ``` 上面的代码使用了 try-with-resources 语句,这样在结束后会自动关闭流。如果不使用 try-with-resources 语句,需要在 finally 块中手动关闭流。 需要注意的是,上面的代码只能复制纯文本文件。如果要复制二进制文件,需要使用字节流(InputStream 和 OutputStream)而不是字符流(Reader 和 Writer)。 ### 回答2: 文件复制通过使用Java中的I/O流可以轻松地实现。下面是一个简单的示例,演示了如何使用I/O流来实现文件复制。 ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopy { public static void main(String[] args) { String sourceFilePath = "source.txt"; // 源文件路径 String targetFilePath = "target.txt"; // 目标文件路径 try { // 创建输入流和输出流 FileInputStream input = new FileInputStream(sourceFilePath); FileOutputStream output = new FileOutputStream(targetFilePath); // 创建一个缓冲区,用于存储读取/写入的数据 byte[] buffer = new byte[1024]; int bytesRead; // 读取源文件中的数据,并将其写入目标文件中 while ((bytesRead = input.read(buffer)) != -1) { output.write(buffer, 0, bytesRead); } // 关闭流 input.close(); output.close(); System.out.println("文件复制完成!"); } catch (IOException e) { System.out.println("发生错误:" + e.getMessage()); } } } ``` 上述代码中,我们首先指定源文件和目标文件的路径。然后,我们使用`FileInputStream`和`FileOutputStream`类创建输入流和输出流。我们还创建一个缓冲区,用于存储读取和写入的数据。 在`while`循环中,我们使用`read()`方法从输入流读取数据,并使用`write()`方法将读取的数据写入输出流。我们还使用`bytesRead`变量来记录实际读取的字节数,以便正确写入到输出流中。 最后,我们在`IOException`中捕获任何可能发生的错误,并将错误消息输出到控制台。如果没有错误发生,则输出"文件复制完成!"的消息。 通过运行上述代码,我们可以将一个文件的内容复制到另一个文件中。请确保源文件存在,并且目标文件不存在,以避免发生意外的覆盖。 ### 回答3: Java使用I/O流完成文件复制非常简单。我们可以使用字节流或字符流来实现文件复制。 使用字节流实现文件复制时,首先需要创建一个文件输入流和一个文件输出流。然后,通过循环从文件输入流中读取字节,并将其写入文件输出流中,实现文件复制。最后记得关闭输入流和输出流。 代码示例: ```java import java.io.*; public class FileCopy { public static void main(String[] args) { // 设置输入文件和输出文件的路径 String inputFilePath = "input.txt"; String outputFilePath = "output.txt"; try { FileInputStream inputStream = new FileInputStream(inputFilePath); FileOutputStream outputStream = new FileOutputStream(outputFilePath); // 定义一个字节数组来存储读取的字节数据 byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } // 关闭输入流和输出流 inputStream.close(); outputStream.close(); System.out.println("文件复制成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 使用字符流实现文件复制时,操作步骤与字节流类似。只是在创建流时需要使用Reader和Writer类,同时要保证源文件和目标文件的编码类型一致。 代码示例: ```java import java.io.*; public class FileCopy { public static void main(String[] args) { // 设置输入文件和输出文件的路径 String inputFilePath = "input.txt"; String outputFilePath = "output.txt"; try { FileReader reader = new FileReader(inputFilePath); FileWriter writer = new FileWriter(outputFilePath); // 定义一个字符数组来存储读取的字符数据 char[] buffer = new char[1024]; int charsRead; while ((charsRead = reader.read(buffer)) != -1) { writer.write(buffer, 0, charsRead); } // 关闭输入流和输出流 reader.close(); writer.close(); System.out.println("文件复制成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 无论使用字节流还是字符流,Java中的I/O流都提供了方便的方法来实现文件复制。通过以上的代码示例,我们可以轻松地完成文件复制操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值