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

实验内容:接收键盘输入的字符串,用FileOutputStream类将字符串写入文件,用FileInputStream类读出文件内容并显示在屏幕上。要求:
1) 程序必须能够从键盘接收字符串并保存在文件中;
2) 程序必须能够读出文件内容,并显示在屏幕上;
实验步骤:
1) 进入Java编程环境;
2) 新建一个Java文件,命名为FileOperation.java;
3) 编写主方法main(),其中实现接收键盘输入功能、文件操作功能和文件内容输出功能;
4) 接收键盘输入,采用下面的代码:
try{
System.out.println("Pleaseinput:");
bytes=System.in.read(outCh,0,100);
}catch(IOExceptione) {System.out.println(e.toString());}
5) 创建文件对象:File myfile=new File(”save.txt”);
6) 创建文件输出流对象:
FileOutputStreamFout = new FileOutputStream(myfile);
7) 写入文件:Fout.write(outCh);其中,outCh为字节数组。
8) 调试运行程序,观察输出结果。

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个回答  推荐于2017-09-29
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOperation {

public static void main(String[] args) {
try {
System.out.println("Pleaseinput:");
File myfile = new File("save.txt");
byte[] outCh = new byte[100];
int bytes = System.in.read(outCh, 0, 100);
FileOutputStream Fout = new FileOutputStream(myfile);
Fout.write(outCh,0,bytes);
byte[] inCh = new byte[bytes];
FileInputStream Fin = new FileInputStream(myfile);
Fin.read(inCh);
System.out.println(new String(inCh));
} catch (IOException e) {
System.out.println(e.toString());
}
}
}

本回答被提问者采纳
相似回答