Java和C#中的文件处理操作
Java和C#是面向对象的编程语言,利用不同的类来创建、删除、打开和关闭文件。
文件是一个存储单位,它在磁盘上以字节序列的形式存储数据。
因此,文件处理是一种技术,涉及使用计算机将数据存储在文件中,以便以后检索。
文件处理操作
Create- 它涉及创建一个新的文件。Read- 它需要读取文件中以前存储的数据。Write- 在这个操作中,一个新的值/内容被存储在内存中。Append- 添加数据涉及向现有文件添加更多信息。Delete- 这个操作需要从内存或磁盘中删除一个现有的文件。
如何对文本文件进行创建、读、写和删除操作
在Java中
我们使用java.io 包中的File 类,它允许我们对文件进行操作。
从你的代码中,你会被提示根据文件操作导入各种包。
例如,只要你想在Java中创建一个文件,你就需要导入java.io.File 类。
import java.io.File;
在Java中创建一个文件
在java中创建一个文件时,我们使用createNewFile() 方法。这个函数返回一个布尔值(结果为真或假)。它也依赖于java.io.File 类。
下面的代码显示了如何在Java中创建一个文件。
import java.io.File;
import java.io.IOException; //importing required packages
public class CreateNewFile {
public static void main(String []args) throws IOException
{
File myFile= new File("New.txt"); //using the file class to create a text file
if(myFile.createNewFile()){ //checking if a new file was created
System.out.println("File is created successfully");
}else{
System.out.println("File creation Error"); //shows an error
}
}
}
在上面的代码片段中,File 类提供了一个createNewFile() 函数来生成一个空文件。
这个方法不接受任何参数,并返回一个布尔值,表明文件是否被创建。
当if 语句中的条件得到满足时,就会创建一个文件;否则,将显示一个错误。
下面是上述代码片断的输出。
The file is created successfully
注意:你需要列出你创建新文件的文件夹的确切路径,无论它有多长。在本例中,创建的文件位于
C:\Users\apond\IdeaProjects。
在Java中写一个文件
要成功地写入一个文件,需要指定数据的类型。这个操作使用FileWriter() 和write() 方法。
下面的代码显示了如何写一个文件。
import java.io.FileWriter;
import java.io.IOException;
public class FileWriting {
public static void main(String[] args) {
String data = "My name is Ashley and I'm writing data into this file.";
// we will write the above string to a file
try{ // We wrap the write operation inside a try-catch block
FileWriter output = new FileWriter("New1.txt");
output.write(data); // Writing our data
System.out.println("Data is entered successfully");
output.close();
}catch(IOException ioe){ // This block is invoked incase of an error
System.out.println("Data entry Error");
}
}
}
在上面的代码中,FileWriter 类的write() 函数被用来向一个文件添加数据。
这种技术被用来将单个字符或字符串写入文件中。在我们的例子中,我们使用了一个名为data ,类型为String 的变量来存储一些内容。
输出。
Data is written successfully
注意:
FileWriter()类与write()方法同时使用。
如果程序运行成功,导航到你的文件夹,检查新插入的数据是否可用。
从一个文件中读取数据
这个操作依赖于FileReader() 和read() 方法。
下面的程序允许人们从一个文件中读取数据。
import java.io.FileReader; // Adding the required imports
import java.io.FileWriter;
import java.io.IOException;
public class FileReading {
public static void main(String[] args) {
char[] data= new char[100]; // Creating a list to store the file's contents
try{
FileReader input = new FileReader("New1.txt"); // Specifying file
input.read(data); //Reading data
System.out.println("Data received from a file");
System.out.println(data); // Printing data
input.close(); //Closing the read operation
}catch(IOException ioe){ // Throws an exception
System.out.println("Error in File Reading ");
}
}
}
为了读取我们先前创建的新文件的内容,我们使用java.io 包。
它包含一个叫做FileReader 的类,可以从一个文件中读取字符。它还扩展了InputStreamReader 类。
FileReader 类有一个read() 方法,它可以读取单个字符或所有字符,并将其存储在data 数组中。
我们在上面的代码中使用了一个data 变量来存储一个字符数组。它最多可以存储100 个字符。
输出。
Data received from a file
My name is Ashley. I am writing data into this file.
追加数据
它使用了append() 方法和FileWriter() 类。
使用StringBuffer ,将char 参数的字符串表示追加到文件中。序列的长度也增加了一个。
在下面的代码片断中,存储在data 变量中的细节被添加到New1.txt 文件内的内容末尾。
下面的代码显示了如何追加数据。
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileAppending {
public static void main(String[] args) {
String data="This is an appended data"; // Data that needs to be appended
try{ //try-catch block
FileWriter output= new FileWriter("New1.txt",true);
output.write(data); //appending data
System.out.println("Data is appended successfully");
output.close();
}catch(IOException ioe){ // Throws exception incase of error
System.out.println("File append error");
}
}
}
输出。
Data is appended successfully
删除一个文件
这个操作涉及到对delete() 方法的使用。
在这个步骤中,我们将复制一个现有的文件,然后创建一个程序来删除它。
下面的代码显示了如何实现删除操作。
package com. ashley;
import java.io.File;
public class Main {
public static void main(String[] args){
File myFile= new File("New2.txt"); //specifying file that needs to be deleted
if (myFile.delete()){ // Calling the delete function
System.out.println("File Deleted Successfully");
}else{ //This section is invoked when the delete operation was unsuccessful.
System.out.println("File deletion Failed");
}
}
输出。
File Deleted Successfully
C#中的读、写、追加和删除操作
System.IO.File 类被利用于读、写、删除和追加操作中。
using System.IO;
在C#中写一个文件
在将数据写入文本文件时,会用到StreamWriter 。它与write() 方法一起使用。
下面的代码说明了如何在C#中写一个文件。
using System;
using System.IO; // Imports
namespace FileIO
{
class Program
{
static void Main(string[] args)
{
Write();
Console.ReadLine();
}
const string filename = "example1.txt"; //Specifying file name
static void Write()
{
StreamWriter sw = new StreamWriter(filename);
sw.WriteLine("Hey am Ashley and am writing a text");
sw.Close(); // Closing the write operation
}
}
}
要访问输出,请在位于
C:\Users\apond\source\repos\FileIO\FileIO\bin\Debug目录中的debug文件夹中查看。
在C#中读取一个文件
StreamReader 类有一个read() 方法,该方法可以从阅读器中读取单个字符或检索所有字符。
下面的代码显示了如何用C#写一个文件。
using System;
using System.IO;
namespace FileIO
{
class Program
{
static void Main(string[] args)
{
Read(); // Invoking the read function
Console.ReadLine();
}
static void Read(){
StreamReader sr = new StreamReader(filename);
string s=sr.ReadToEnd(); // Reading content
Console.WriteLine(s); // Printing output
sr.Close();
}
}
}
输出。
Hello I'm Ashley and I'm writing a text
追加一个文件
Append() 函数用于打开一个现有的文件并添加新的文本。
下面是一个如何追加文件的示例代码。
using System;
using System.IO;
namespace FileIO
{
class Program
{
static void Main(string[] args)
{
Append(); // Invoking the append function
Console.ReadLine();
}
static void Append()
{
StreamWriter sw = new StreamWriter(filename, true);
for (int i = 0; i < 4; i++){ // Using a for loop to iterate through the data.
sw.WriteLine(i); // Adding data to file
}
sw.Close();
}
}
}
输出。
Hey am Ashley and am writing a text
0
1
2
3
上面的输出显示,文件被循环追加了。
删除一个文件
下面的代码显示了如何追加一个文件。
using System;
using System.IO;
namespace FileIO
{
class Program
{
static void Main(string[] args)
{
const string filename = "example2.txt";
Console.WriteLine("Deleting file!");
File.Delete(filename); // Calling the delete function and passing in the file
}
}
}
在上面的代码中,我们使用'delete()'方法永久性地删除了'example2.txt'文件。
管理文件的类和方法
在Java中
FileWriter()- 这个类是面向字符的,它将数据写入文件。FileReader()- 该类允许人们以字节格式从文件中读取数据。delete()- 这个函数在删除一个文件后返回一个布尔值。getName()- 这个方法是字符串类型的,给出的是文件名。read()- 这个技术从一个文件中读取数据。write()- 这个函数给出了文件的输出。IOException- 这个异常是用来处理程序中的故障或错误。
在C#中
StreamWriter()- 这个类用于字符输出。StreamReader()- 这个类用于字符输入。Close()- 这个方法关闭一个打开的文件。
文件处理的重要性
- 程序可以处理和使用数据。
- 它使数据能够被保存到计算机内存中。
- 信息也可以存储在文件中,以后再检索。
总结
在本教程中,我们了解了Java和C#中不同的文件处理技术。我们还讨论了用于文件操作的重要类和方法。
因此,你可以利用这些知识在Java和C#程序中实现文件处理操作。