编写一个Java Application程序
(1)从命令行后输入任意个参数,把参数用&符号连接成新的字符串,查找新的字符串中是否有gench,如果有,在控制台输出“上海建桥学院“
(2)将新的字符串写到文件D:\ myfile.txt中;
(3)要求处理该程序中所有可能出现的异常。
提示:
参考StringBuffer类里面的equals、append等方法
(1)从命令行后输入任意个参数,把参数用&符号连接成新的字符串,查找新的字符串中是否有gench,如果有,在控制台输出“上海建桥学院“
(2)将新的字符串写到文件D:\ myfile.txt中;
(3)要求处理该程序中所有可能出现的异常。
提示:
参考StringBuffer类里面的equals、append等方法
2015-12-03
public static void main(String[] args) { Scanner input = new Scanner(System.in); String inputStr=input.next(); String replaceStr=inputStr.replaceAll(",", "&"); String[] str = replaceStr.split("&"); for (String s : str) { if(s.equals("gench")){ System.out.println("上海建桥学院"); } } File file = new File("d:/myfile.txt"); try { OutputStream outputStream = new FileOutputStream(file); OutputStreamWriter writer = new OutputStreamWriter(outputStream); writer.append(replaceStr); writer.close(); outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
package hello;
import java.io.*;
import java.util.Scanner;
/*import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;*/
public class Example03{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inputStr=input.next();
input.close();
String replaceStr=inputStr.replaceAll(",", "&");
String[] str = replaceStr.split("&");
for (String s : str) {
if(s.equals("gench")){
System.out.println("上海建桥学院");
}
}
File file = new File("d:/myfile.txt");
try {
OutputStream outputStream = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
writer.append(replaceStr);
writer.close();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
举报