1、编写java程序,将用户在命令行窗口键盘输入的内容写到一个新建的txt文件中 求大神!!!!
发布网友
发布时间:2022-05-05 06:07
我来回答
共4个回答
热心网友
时间:2023-10-12 06:18
package com.text;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class TextCase {
public static void main(String[] args) {
String path = "d:\\1.txt";
String s=readTxtFile(path);
String news = "";
for(int i=0;i<s.length();i++){
//如果是大写
if(s.substring(i,i+1).equals(s.substring(i,i+1).toUpperCase())){
news = news + s.substring(i,i+1).toLowerCase();
}
else{
news = news + s.substring(i,i+1).toLowerCase();
}
}
writeTxtFile(path,news);
System.out.print("转换后文本:"+news);
}
//读文件
public static String readTxtFile(String filePath){
String result="";
try {
//String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file));//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println("原文:"+lineTxt);
result = result+lineTxt;
}
read.close();
bufferedReader.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return result;
}
//写文件
public static void writeTxtFile(String filePath,String result){
try {
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
if(result!=null){
bufferedWriter.write(result);
bufferedWriter.close();
}
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("写文件出错");
e.printStackTrace();
}
}
}
热心网友
时间:2023-10-12 06:19
下面的两个方法可以解决你的问题,你放在main函数里面执行一下
//将大写转成小写,前提是这个文件存在D:/lin/testfile.txt
public static void ToLowerCase() throws IOException{
File file = new File("D:/lin/testfile.txt");
InputStreamReader in = new InputStreamReader(new FileInputStream(file),"gbk");
BufferedReader bufferedReader = new BufferedReader(in);
String lineTxt = null;
String test = "";
while((lineTxt = bufferedReader.readLine()) != null){
test = lineTxt.toLowerCase();
System.out.println(test);
}
FileWriter fw = new FileWriter(file);
fw.write("");
fw.close();
in.close();
OutputStream out = new FileOutputStream(file,true);
out.write(test.getBytes("GBK"));
out.close();
}
//将在控制台中输入的字符保存在txt文件中
public static void save() throws IOException{
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
(new File("D:/lin/testfile.txt")).delete();
//在d盘上创建一个名为testfile的文本文件
File f = new File("D:/lin"+File.separator+"testfile.txt");
//用FileOutputSteam包装文件,并设置文件可追加
OutputStream out = new FileOutputStream(f,true);
out.write(input.getBytes("GBK"));
out.close();
}
热心网友
时间:2023-10-12 06:19
public static void main(String[] args) {
String filepath1 = "D:\\text.txt";
String filepath = "D:\\test.txt";
BufferedWriter bw = null;
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath1)));
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filepath)));
String lin = "";
while (!(lin = br.readLine()).equals("exit")) {
bw.write(lin.toLowerCase());
bw.newLine();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(bw != null){
bw.flush();
bw.close();
}
if(br != null)
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
热心网友
时间:2023-10-12 06:20
见附件