1、编写java程序,将用户在命令行窗口键盘输入的内容写到一个新建的txt文件中 求大神!!!!
2、编写一个java程序读取一个txt文件(该文件只包含英文内容),将该文件中包含的大写字母转换成小写字母,并保存该文件求大神帮忙!!!好人一生平安!!!!...
2、编写一个java程序读取一个txt文件(该文件只包含英文内容),将该文件中包含的大写字母转换成小写字母,并保存该文件
求大神帮忙!!!好人一生平安!!!! 展开
求大神帮忙!!!好人一生平安!!!! 展开
展开全部
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();
}
}
}
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();
}
}
}
展开全部
下面的两个方法可以解决你的问题,你放在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();
}
//将大写转成小写,前提是这个文件存在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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
见附件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询