接收键盘输入的字符串,用FileInputStream类将字符串写入文件,用FileOutputStream类读出文件内容显示在屏

如题所述

1、创建一个控制台应用程序,用于演示string类的Contains包含方法。

2、在Main方法中定义一个string变量var str = "Hello, Hello World!"。

3、使用Contains方法,检测字符串str中是否含有he,Contains方法查找到指定内容就返回true,否则返回false。

4、使用Contains方法,检测字符串str中是否含有He。

5、在调试模式下运行,可以看到结果是“存在”,这就表示Contains方法判断存在是区分大小写的。

注意事项:

字符串或串(String)是由数字、字母、下划线组成的一串字符。一般记为 s=“a1a2···an”(n>=0)。它是编程语言中表示文本的数据类型。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-04-25
楼主反了,FileOutputStream是写文件的,FileInputStream是读文件的
源代码
import java.util.*;
import java.io.*;

public class Ceshi{
public static void write(){
OutputStream os = null;
try{
os = new FileOutputStream(new File("input.txt"));
}catch(Exception e){
e.printStackTrace();
}
Scanner sc = new Scanner(System.in);
System.out.println("输入一行内容,如果该行内容仅为@则结束输入");
boolean flag = true;
while(flag){
String str = sc.nextLine();
if("@".equals(str)){
flag=false;
}
else{
byte[] b = (str+"\r\n").getBytes();
try{
os.write(b);
}catch(IOException e){
e.printStackTrace();
}
}
}
try{
os.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static void read(){
InputStream is = null;
try{
is = new FileInputStream(new File("input.txt"));
}catch(Exception e){
e.printStackTrace();
}
StringBuffer sb = new StringBuffer("");
int temp = 0;
try{
while((temp=is.read())!=-1){
sb.append((char)temp);
}
is.close();
}catch(IOException e){
e.printStackTrace();
}
System.out.println(sb);
}
public static void main(String[] args){
write();
read();
}

}
第2个回答  2011-04-26
public static void main(String argsp[])
{
String s="e:\\aaa.txt";
File f = new File(s);
String sss="aaaa";

if(f.exists())
{
try {
FileInputStream fm = new FileInputStream(f);
BufferedInputStream bm = new BufferedInputStream(fm);

FileOutputStream fs = new FileOutputStream(f, false);
BufferedOutputStream bs = new BufferedOutputStream(fs);

bs.write(sss.getBytes());

bs.flush();
fs.close();

byte b[] = new byte[1024];
int i=0;
if(bm.available()!=0)
{
i=bm.read(b);
sss=new String(b,0,i);
}

System.out.println(sss);
bm.close();

} catch (FileNotFoundException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

}
else
{
try {
f.createNewFile();

} catch (IOException e) {

e.printStackTrace();
}
}
}

希望是这样 eclipse 编译本回答被提问者和网友采纳
相似回答