当前位置 : 主页 > 编程语言 > java >

2022年6月28日——Java案例(4)

来源:互联网 收集:自由互联 发布时间:2022-07-04
文末的小案例演示效果: 获取文件信息: package day_04 ; import java . io . File ; import java . io . IOException ; public class Demo01 { public static void main ( String [] args ) throws IOException { String fileName = "你好

文末的小案例演示效果:

2022年6月28日——Java案例(4)_txt文件


获取文件信息:

package day_04;

import java.io.File;
import java.io.IOException;

public class Demo01 {

public static void main(String[] args) throws IOException {
String fileName = "你好.txt";//文件名称(注意:是文件的名称和文件的后缀)
String filePath = "src\\day_04";//文件的路径(也就是文件所在的位置)
File file = new File(filePath,fileName);
System.out.println(file.getPath());//获取文件的路径(这个路径,与上边的filePath有关,如果上边是相对路径,这里就是相对路径;如果上边是绝对路径,这里就是绝对路径)
System.out.println(file.getName());//获取文件的名称
System.out.println(file.getAbsolutePath());//获取文件的绝对路径(在电脑中的唯一位置)
boolean exists = file.exists();//用于判断,当前的这个文件或文件夹是否存在
boolean newFile = file.createNewFile();//用于创建文件
boolean mkdirs = file.mkdirs();//用于创建文件夹
//。。。
}
}

读取txt文件:

package day_04;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
* 读取txt文件
*/
public class Demo02 {
public static void main(String[] args) {
String fileName = "a.txt";
String filePath = "D:\\test\\txt";
readerTxt(fileName, filePath);
}

public static void readerTxt (String fileName, String filePath) {
try {
File file = new File(filePath+"/"+fileName);
FileInputStream fileInputStream = new FileInputStream(file);
//charsetName,是读取文件的字符编码,如果有乱码可以进行更改
// InputStreamReader read = new InputStreamReader(fileInputStream, "GBK");
InputStreamReader read = new InputStreamReader(fileInputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(read);
String lineData;
while((lineData= bufferedReader.readLine()) != null){//读取文件,每次读取一行数据
System.out.println(lineData);
}
read.close();
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
}

内容书写txt文件:

package day_04;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

public class Demo03 {
public static void main(String[] args) {
Demo03 demo03 = new Demo03();
demo03.writTXT("你好","D:\\test\\txt\\a.txt", true);
}

public void writTXT(String content, String filePath, boolean isCover){
try{
File file = new File(filePath);
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();//创建文件夹
}
if (!file.exists()){
file.createNewFile();//创建文件
}
FileWriter fileWriter = new FileWriter(filePath, isCover);
BufferedWriter bw = new BufferedWriter(fileWriter);
bw.newLine();
bw.write(content);
fileWriter.flush();
bw.close();
fileWriter.close();
System.out.println("内容添加成功");
}catch (Exception e){
System.out.println("内容添加失败");
e.printStackTrace();
}
}
}

加密:

package day_04;


import java.math.BigInteger;
import java.security.MessageDigest;

public class Demo04 {

public static void main(String[] args) {
String data = enc("张三");
System.out.println(data);
}

public static String enc(String str){//str为加密的数据,返回的结果为加密后的数据
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
return new BigInteger(1, md.digest()).toString(16);
}catch (Exception e){
return "加密出现错误";
}
}

}

txt文件的读取和内容添加数据演示小案例

package day_04;

import java.io.*;
import java.util.Scanner;

public class Demo05 {

public static void main(String[] args){
System.out.println("实现一个读写的功能:");
System.out.println("描述:实现对txt文件的操作,在txt文件中添加内容,然后查看txt文件的内容");
Scanner scanner = new Scanner(System.in);
String fileName = "a.txt";
String filePath = "D:\\test\\txt";
boolean flag;
while (true){
System.out.println("读取文件,请输入读取文件的字符编码:1、UTF-8\t2、GBK,如果跳过,则输入0,将默认设置为1的选项");
String next = scanner.next();
if ("0".equals(next)){
readerTxt(fileName,filePath,null);
}else if ("1".equals(next)){
readerTxt(fileName,filePath,"UTF-8");
}else if ("2".equals(next)){
readerTxt(fileName,filePath,"GBK");
}else {
System.out.println("您的输入有误!");
continue;//跳过本次,重新执行下一次
}
System.out.println("请输入您要添加的内容:");
String data = scanner.next();
System.out.print("请问你是否要覆盖之前写的内容:(1.是/2.否)");
int i = scanner.nextInt();
if (i == 1){
writTXT(data,filePath+"/"+fileName,false);//删除原有的内容,重新书写新的内容
}else if (i == 2){
writTXT(data,filePath+"/"+fileName,true);//在原有的内容上,进行追加
}else {
System.out.println("您的输入有误");
}

}


}

public static void writTXT(String content, String filePath, boolean isCover){
try{
File file = new File(filePath);
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();//创建文件夹
}
if (!file.exists()){
file.createNewFile();//创建文件
}
FileWriter fileWriter = new FileWriter(filePath, isCover);
BufferedWriter bw = new BufferedWriter(fileWriter);
bw.newLine();
bw.write(content);
fileWriter.flush();
bw.close();
fileWriter.close();
System.out.println("内容添加成功");
}catch (Exception e){
System.out.println("内容添加失败");
e.printStackTrace();
}
}

public static void readerTxt (String fileName, String filePath, String encoding) {
try {
if (null == encoding || "".equals(encoding)){
encoding = "UTF-8";
}
File file = new File(filePath+"/"+fileName);
FileInputStream fileInputStream = new FileInputStream(file);
//charsetName,是读取文件的字符编码,如果有乱码可以进行更改
// InputStreamReader read = new InputStreamReader(fileInputStream, "GBK");
InputStreamReader read = new InputStreamReader(fileInputStream, encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineData;
while((lineData= bufferedReader.readLine()) != null){//读取文件,每次读取一行数据
System.out.println(lineData);
}
read.close();
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}

}

文中的小程序,难免存在,有bug之类的,一起交流,提升。。。

如有不足之处,希望您能提出一些宝贵的建议,谢谢

【文章出处:日本大带宽服务器 http://www.558idc.com/jap.html 欢迎留下您的宝贵建议】
上一篇:Ubuntu环境下载OpenJDK11源码
下一篇:没有了
网友评论