JAVA文件读写例题实现过程解析
更新时间:2020年06月29日 11:45:08 作者:JC97
这篇文章主要介绍了JAVA文件读写例题实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
练习
有这样的一个words数组,数组中每个字符串的格式为“词性:单词”
String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
根据单词性质动词verb全部存入verb.txt文件中
根据单词性质名词noun全部存入noun.txt文件中
package readandwrite;
/*1.有这样的一个words数组,数组中每个字符串的格式为“词性:单词”
String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
根据单词性质动词verb全部存入verb.txt文件中
根据单词性质名词noun全部存入noun.txt文件中
*/
import java.io.*;
public class FileReadAndWrite {
public static void main(String args[]) throws IOException {
//WORDS数组
String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
FileOutputStream outFile1 = new FileOutputStream("verb.txt");
FileOutputStream outFile2 = new FileOutputStream("noun.txt");
OutputStream out1 = new BufferedOutputStream(outFile1);
OutputStream out2 = new BufferedOutputStream(outFile2);
for(int i=0;i<words.length;i++){
if(words[i].startsWith("verb")){
byte[] bytes1 = words[i].getBytes();
out1.write(bytes1);
}
if(words[i].startsWith("noun")){
byte[] bytes2 = words[i].getBytes();
out2.write(bytes2);
}
}
out1.close();
out2.close();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
使用Spring Initializr方式如何快速构建Spring Boot项目
Spring lnitializr是一个Web应用,它提供了一个基本的项目结构,能够帮助我们快速构建一个基础的Spring Boot项目,本文分步骤讲解如何使用Spring Initializr方式构建Spring Boot项目,感兴趣的朋友跟随小编一起看看吧2023-08-08
SpringBoot整合Mybatis实现高德地图定位并将数据存入数据库的步骤详解
这篇文章主要介绍了SpringBoot整合Mybatis实现高德地图定位并将数据存入数据库的步骤详解,本文分步骤通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-01-01
Spring AI Alibaba接入大模型时的依赖问题小结
文章介绍了如何在pom.xml文件中配置SpringAI Alibaba依赖,并提供了一个示例pom.xml文件,同时,建议将Maven仓库镜像设置为阿里云以提高下载速度,具体配置方法跟随小编一起学习下吧2025-02-02


最新评论