62,635
社区成员




import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.Scanner;
public class StringAddressing{
public static void main(String args[]){
new chuli().run();
}
}
class chuli {
void run(){
char[] s ;
System.out.println("请分别输入a,b,c执行以下功能:");
System.out.println("\na表示创建文件并且产生随机字符串保存;\nb表示查找所产生的随机字符串的内容;\nc表示替换字符串;");
char []s0={'a','b','c'};
Scanner read=new Scanner(System.in);
for(int i=0;i<3;i++){
try {
s=read.next().toCharArray();
if(s[0]==s0[i]){
switch(s[0]){
//a表示创建文件并且产生随机字符串保存;
//b表示查找所产生的随机字符串的内容;
//c表示替换字符串;
case 'a': System.out.println("您的输入为a");runA();break;
case 'b': System.out.println("您的输入为b");runB();break;
case 'c': System.out.println("您的输入为c");runC();break;
}
}
else{
System.out.println("请按照a,b,c顺序输入!现在该输入"+s0[i]);
i--;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
read.close();
}
/*
* a表示创建文件并且产生随机字符串保存
*/
void runA(){
File file =new File("e:\\course_1.txt");
try {
if(!file.exists()){
file.createNewFile();
}
else{
System.out.println("该文件已经存在!正在使用已存在的文件进行操作···");
}
Random random=new Random();
String s1="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .";
String s=new String();
StringBuffer sb=new StringBuffer();
int m=random.nextInt(100)+10;
for(int i=0;i<m;i++){
int n=random.nextInt(64);
sb.append(s1.substring(n,n+1));
}
s=sb.toString();
Thread.sleep(1000);
System.out.println("已经产生随机字符串:"+s);
FileWriter out1=new FileWriter(file);
BufferedWriter out2=new BufferedWriter(out1);
out2.append(s);
Thread.sleep(1000);
System.out.println("已经将随机字符串保存到了e:\\course_1.txt中!");
out2.close();
out1.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* b表示查找所产生的随机字符串的内容
*/
void runB(){
File file =new File("e:\\course_1.txt");
try {
FileReader in1=new FileReader(file);
BufferedReader in2=new BufferedReader(in1);
String s=new String();
while((s=in2.readLine())!=null){
System.out.println("e:\\course_1.txt中的内容为:"+s);//..........
}
in2.close();
in1.close();
Thread.sleep(1000);
System.out.println("请输入要查找的内容:");
Scanner read=new Scanner(System.in);
String find=read.nextLine();//find
System.out.println("正在进行字符串查找···");
Thread.sleep(1000);
read.close();
for(int i=0;i<s.length()-find.length();i++){
if(s.regionMatches(i, find, 0, find.length())){
System.out.println("相同部分起始位置\n"+i+"\n");
}
}
String sh=new String("hehe");//sh为查找后的
File f=new File("e:\\course_2.txt");
if(!f.exists()){
f.createNewFile();
}
FileWriter out1=new FileWriter(f);
BufferedWriter out2=new BufferedWriter(out1);
out2.write(sh);
System.out.println("已经将查找后的字符串保存到了e:\\course_2.txt中!");
out2.close();
out1.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* c表示替换字符串;
*/
void runC(){
File file =new File("e:\\course_1.txt");
try {
FileReader in1=new FileReader(file);
BufferedReader in2=new BufferedReader(in1);
String s=new String("0");
while((s=in2.readLine())!=null){
System.out.println("e:\\course_1.txt中的内容为:"+s);//s..........
}
in2.close();
in1.close();
Thread.sleep(1000);
System.out.println("请输入要替换的内容:");
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
String find=new String("0");
find=read.readLine();
//find为要替换的内容
System.out.println("请输入要用来替换的内容:");
String find1=new String("0");
find1=read.readLine();
//find1为要用来替换的内容
read.close();
System.out.println("正在进行字符串替换···");
Thread.sleep(1000);
String sh=new String();
//sh
sh=s.replaceAll(find, find1);
File f=new File("e:\\course_3.txt");
if(!f.exists()){
f.createNewFile();
}
FileWriter out1=new FileWriter(f);
BufferedWriter out2=new BufferedWriter(out1);
out2.write(sh);
System.out.println("已经将替换后的字符串保存到了e:\\course_2.txt中!");
out2.close();
out1.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}