/*从键盘输入一个文件名在屏幕显示文件内容*/
import java.util.Scanner;
import java.io.File;
import java.io.*;
public class test13_3 {
public static void Print(File file)
{
try{
BufferedReader bu = new BufferedReader(new FileReader(file));
//构造一个BufferedReader类来读取文件
String s = null;
while((s = bu.readLine())!=null)//readLine方法,一次读一行
System.out.println(s);
bu.close();
}catch(Exception e)
{
e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
}
}
public static void main(String[] args) {
Scanner In = new Scanner(System.in);
System.out.println("请输入文件名:");
String str = In.next();
String Str1 = "D:\\" + str + ".txt";//文件具体目录
File file = new File(Str1);
Print(file);
}
}
import java.util.*;
import java.io.*;
public class test13_4 {
public static void WordCount(String Str) {
String Str1=new String();
for(int i=0;i<Str.length();i++){
if(Str.charAt(i)>='a'&&Str.charAt(i)<='z'||Str.charAt(i)>='A'&&Str.charAt(i)<='Z')
Str1+=Str.charAt(i);
else if(Str.charAt(i-1)>='a'&&Str.charAt(i-1)<='z'||Str.charAt(i-1)>='A'&&Str.charAt(i-1)<='Z')
Str1+=' ';
}
String[] Str2=Str1.split(" ");
int count;
for(int i=0;i<Str2.length;i++) {
System.out.print(Str2[i]+" ");
if((i+1)%10==0)
System.out.println();
}
System.out.println("文章中单词个数为:"+Str2.length);
}
public static void SumCount(File file) throws IOException {
try {
BufferedReader bu=new BufferedReader(new FileReader(file));
String s=null;
String S1="";
while((s=bu.readLine())!=null)
S1=S1+s+" ";
//System.out.println(S1);
bu.close();
WordCount(S1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException
{
Scanner In=new Scanner(System.in);
System.out.print("请输入文本文件名:");
String str=In.next();
String str1="D:\\学习代码\\java-learning\\java学习\\第十三章1\\"+str+".txt";
File file=new File(str1);
SumCount(file);
}
}
import java.util.*;
import java.io.*;
//利用随机函数产生20个整数,按小到大的顺序排序后写入文件,然后从文件中读取整数并显示。分别用顺序文件和随机文件的读写方式进行编程测试。
public class test13_5 {
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<Integer>();
for (int i = 1; i <= 20; i++)
a.add((int) (Math.random() * 100));
for (int i = 0; i < a.size(); i++)
System.out.print(a.get(i) + " ");
a.sort(null);
System.out.println();
for (int i = 0; i < a.size(); i++)
System.out.print(a.get(i) + " ");
//顺序文件
try {
FileWriter fw = new FileWriter("charset.txt");
for (int i = 0; i < a.size(); i++)
fw.write(Integer.toString(a.get(i)) + " ");//.write输出ASCII英文字符,不能直接输出整形
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println();
ArrayList<Integer> a1 = new ArrayList<Integer>(); //读取的数据存储在a1中
try {
BufferedReader in = new BufferedReader(new FileReader("charset.txt"));//构造一个BufferedReader类来读取文件
String s = null;
while ((s = in.readLine()) != null)//readLine方法,一次读一行
{
String[] temp = s.split(" ");
for (int j = 0; j < temp.length; j++)
a1.add(0, Integer.valueOf(temp[j]));
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < a.size(); i++)
System.out.print(a1.get(i) + " ");
System.out.println();
//随机文件
StringBuffer sb=new StringBuffer();
for (int i = 0; i < a.size(); i++) {
sb.append(a.get(i));
sb.append(" ");
}
sb.append("\n");
System.out.println(sb);
try {
RandomAccessFile fio = new RandomAccessFile("charset1.txt", "rw");
fio.seek(fio.length()); //移动到文件尾部
fio.writeBytes(sb.toString());
fio.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}