实验目的:
学习使用Eclipse编写运行Java程序,综合应用Java的字符串操作和输入输出流的相关操作
实验内容:
已知字符串str的值为 3|Four Rooms (1995)|01-Jan-1995||http://us.imdb.com(1995)|0|0|0|0|0|1|0|0,利用String类的相关方法,实现如下功能:
1.(1)以“|”作间隔划分子串,输出全部子串;
(2)利用正则表达式过滤掉无关内容后,输出s中出现的英文单词;
(3)输出所有“1995”的位置编号。
2. 利用BufferedReader、BufferedWriter等流类的相关方法,实现如下功能:
(1)把字符串“中国”、“山东”、“青岛”分三行写入文本文件A.txt;
(2)从A.txt中读出字符串,输出“中国-山东-青岛”;
(3)输出C:\Windows\System32\drivers\etc目录下的文件数目,统计并输出每个文件拥有文本的行数。
3. 利用LinkedList或ArrayList泛型类实现如下功能:
(1)初始化链表list,存放字符串数据“Java”、“真”、“有趣”、“!”;
(2)用迭代器方法和普通方法遍历输出list中的全部数据。
具体代码:
package com.report;
public class Report4_1 {
public static void main(String[] args) {
String str="3|Four Rooms (1995)|01-Jan-1995||http://us.imdb.com(1995)|0|0|0|0|0|1|0|0";
String [] str1=str.split("[|]+");//以"|"作间隔划分子串
for(String s:str1){
System.out.println(s);//输出全部子串
}
System.out.println("-----------------------------------");
str1=str.split("[\\s\\d\\p{Punct}]+");//过滤掉除字母之外的无关内容
for(String s:str1){
System.out.println(s);
}
System.out.println("-----------------------------------");
int a=str.indexOf("1995");
while(a>0){
System.out.println(a);
a=str.indexOf("1995",a+"1995".length());//改变搜索位置,寻找下一个匹配
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class Report4_2_1 {
public static void main(String[] args) throws Exception{
String path="C:\\Users\\qq\\Desktop\\A.txt";
File file=new File(path);
FileWriter fileWrite =new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fileWrite);
String [] str={"中国","山东","青岛"};
for(String s:str){
bw.write(s);
bw.newLine();
}
bw.close();
FileReader fileReader=new FileReader(file);
BufferedReader br=new BufferedReader(fileReader);
String line=br.readLine();
int count=0;
while(line!=null){
if(count==0){
System.out.print(line);
}else{
System.out.print("-"+line);
}
line=br.readLine();
count++;
}
br.close();
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Report4_2_2 {
public static void main(String[] args) throws Exception {
String path="C:\\Windows\\System32\\drivers\\etc\\";
File file=new File(path);
File [] files =file.listFiles();
System.out.println("拥有文件数目:"+files.length);//输出文件数目
for(File f:files){
FileReader fileReader=new FileReader(f);
BufferedReader br=new BufferedReader(fileReader);
int count=0;
String line=br.readLine();
while(line!=null){
count++;
line=br.readLine();
}
fileReader.close();
br.close();
System.out.println(f.getAbsolutePath()+"拥有文件行数:"+count);
}
}
}