实验二 常用Java类
【实验目的】
1.掌握String类和StringBuffer类的使用; 2.掌握包装类的使用;
3. 掌握集合类ArrayList对象、Vector对象的使用。 【实验准备】
一、复习配套教材相关章节的内容; 二、预习本次实验; 【实验内容】
1.编写application程序,判断一个字符串是否是回文(指顺读和倒读都一样的词语)。提示:利用StringBuffer类的reverse()方法。
2. 随机输入一个人的姓名(中国人习惯,单姓),然后分别输出姓和名。
3
3、编写一个方法,返回一个double类型的二维数组,数组中的元素通过解析字符串参数获得。如字符串参数:“1,2;3,4,5;6,7,8”,对应的数组为:
d[0,0]=1.0 d[0,1]=2.0
d[1,0]=3.0 d[1,1]=4.0 d[1,2]=5.0 d[2,0]=6.0 d[2,1]=7.0 d[2,2]=8.0
4、编写一个程序,使用ArrayList存储客户的邮件地址。地址中应包括姓名、街道、市(县)、省(自治区)、国家(地区)。然后通过命令行输出ArrayList中的内容。
【总结与体会】
4
实验三 Java 输入输出流
【实验目的】
1.对文件进行字符、字节和字符串读写操作。 2.复制文件。
3.查找有关文件和目录的信息 【实验准备】
一、复习配套教材相关章节的内容; 二、预习本次实验; 【实验内容】
1.编写一个程序,读取文件test1.txt的内容并在控制台输出。如果源文件不存在,则显示相应的错误信息。
public static void main(String[] args) { try {
FileInputStream fis=new
FileInputStream(\ File f=new File(\ long filelength=0; if(f.exists())
filelength=f.length();
byte[] buffer=new byte[(int)filelength]; /*
int i=0;
int x=fis.read(); while(x!=-1) {
buffer[i]=(byte)x; i++;
5
}
x=fis.read(); }
String s=new String(buffer); System.out.println(s); */
/*fis.read(buffer);
String s=new String(buffer); System.out.println(s);*/
fis.read(buffer, 0, (int)filelength); String s=new String(buffer); System.out.println(s);
}catch(Exception e) { }
2、编写一个程序实现如下功能,从当前目录下的文件fin.txt中读取80 个字节(实际读到的字节数可能比80少)并将读来的字节写入当前目录下的文件fout.txt中。
ss FileStream {
public static void main(String args[]){
try {
File inFile=new File(\ File outFile=new File(\
6
FileInputStream f1=new FileInputStream(inFile);
FileOutputStream f2=new FileOutputStream(outFile); int c;
while((c=f1.read())!=-1) {
f2.write(c); } //
// f1.close(); // f2.close();
}catch(FileNotFoundException e) {
System.out.println(\\
}catch(IOException e) {
System.err.println(\ } } }
3.使用Java的输入/输出流技术将一个文本文件的内容按行读出,每读出一行就顺序添加行号,并写入到另一个文件中。
4.编写一个程序,接收从键盘输入的数据,并把从键盘输入的内容写到input.txt文件中,如果输入“quit”则程序结束。
5、编写一个程序实现如下功能,文件fin.txt是无行结构(无换行
7