java 中文件写入读出

对文件的操作可以利用 File 类进行操作。
1字节=8位
file.length() 返回的是字节 byte
工具类一般要用静态方法。
读取文件函数
public static String readFile(String filename) throws IOException{
String content="";
File file=new File(filename);
InputStream inputStream=new FileInputStream(file);//向上转型 字节-》字节流
//从流中取得数据
int length=(int) file.length();//的到文件长度
// for(int i=0;i<length;i++){ //第一种读取方法--效率低,频繁与硬盘交流
// int data=inputStream.read();
// content+=(char)data;
// System.out.println((char)data);
// }
byte[] b=new byte[length];//第二中读取方法
inputStream.read(b);
content=new String(b,"utf-8");//可以通过String构造函数进行对字节的处理,后面指定编码规则
inputStream.close();
return content;
}
写入文件函数
public static void writeFile(String content,String filename) throws IOException{
File file=new File(filename);
OutputStream outputstream=new FileOutputStream(file,true);//后面加个true将为一个追加模式;
byte[] b=content.getBytes("utf-8");//指定编码格式
outputstream.write(b);
outputstream.close();
}
缓冲流(减少对硬盘的访问,就是从硬盘读取后放在缓冲流,然后重缓冲流进行操作)
程序操作:
InputStream inputstream;
BufferedInputStream buffer=new BufferInputStream(inputStream);
buffer.read(b);

OutputStream outputstream;
BufferedOutputStream buffer=new BufferOutputStream(outputStream);
buffer.write(b);
关闭的时候先关闭缓冲流后再关闭输入流
字符流
(最大好处就是不用关心字符的编码问题)
读出数据:
public static String readFileByCharacterStream(String filename) throws IOException{
String connent=" ";
File file=new File(filename);
Reader reader=new FileReader(file);
int length=(int) file.length();
char [] charArray=new char[length];
reader.read(charArray);
reader.close();
connent=new String(charArray);
return connent;
}
使用缓冲流读出数据
public static String readFileByCharacterStreamUsingBufferStream(String filename) throws IOException{
String connent=" ";
File file=new File(filename);
Reader reader=new FileReader(file);
BufferedReader bufferreader=new BufferedReader(reader);
StringBuilder build=new StringBuilder();//不可以在有多线程的地方使用,在多线程用StringBuffer
String line=bufferreader.readLine();
while(line!=null){
build.append(line);
line=bufferreader.readLine();
}
bufferreader.close();
reader.close();
connent=build.toString();
return connent;
}
使用缓冲流写入数据
public static void WriteFileByCharacterString(String connent,String descFile) throws IOException{
File file=new File(descFile);
Writer write=new FileWriter(descFile,true);
BufferedWriter bufferwriter=new BufferedWriter(write);
bufferwriter.write(connent);
bufferwriter.close();
write.close();
}

读写文件的基本步骤

字节流
File->InputStream(FileInputStream) || OutputStream(FileOutputStream)->BufferedInputStream|| BufferedOutputStream

字符流
File->Reader(FileReader) || Writer(FileWriter) ->BufferedReader || BufferedWriter

记得要按栈的方式 先开后关来 关闭这些流!!!!!!!!!!!!!!!




### 如何在编程中写入和读取文件 对于文件写入与读取操作,在不同编程语言中有不同的实现方式。这里将以 Python 和 Java 为例来展示具体的代码示例。 #### 使用Python进行文件写入与读取 Python 提供了一种简单而直观的方法来进行文件的操作: ##### 文件写入 当需要向文件写入数据时,可以使用 `open()` 函数打开文件并指定模式为 `'w'` 或者 `'a'` 来覆盖或追加内容到文件中[^2]。 ```python with open('example.txt', 'w') as file: file.write("Writing data into this text file.\n") ``` ##### 文件读取 为了从文件中读取数据,同样可以通过 `open()` 函数以只读模式 (`'r'`) 打开文件,并通过 `.read()`, `.readline()`, 或者迭代器的方式逐行读取文件中的每一行。 ```python with open('example.txt', 'r') as file: content = file.read() print(content) ``` #### 使用Java进行JSON文件写入与读取 如果目标是在 Java 中处理 JSON 文件,则可借助像 Gson 这样的库完成此任务[^4]。 ##### JSON文件写入 下面是一个简单的例子展示了如何创建一个新的 JSON 对象并将它保存至文件中: ```java import com.google.gson.Gson; public class Main { public static void main(String[] args) throws IOException { String jsonContent = new Gson().toJson(new Message("Hello")); try (FileWriter writer = new FileWriter("message.json")) { writer.write(jsonContent); } } private static class Message { private final String message; Message(final String msg){ this.message=msg; } } } ``` ##### JSON文件读取 要解析已有的 JSON 文件,可以从磁盘加载其字符串表示形式再转换回对象实例: ```java import java.io.FileReader; import com.google.gson.Gson; public class Main { public static void main(String[] args) throws FileNotFoundException { try (final FileReader reader = new FileReader("message.json")){ final Message obj = new Gson().fromJson(reader, Message.class); System.out.println(obj.getMessage()); } catch (IOException e) { e.printStackTrace(); } } private static class Message { private String message; // Getter method for accessing the value of message field. public String getMessage(){ return this.message; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值