package demo13;
import java.io.*;
import java.util.*;
public class text1 {
String dir="d:\\mytext.txt";
int max=-1;
public void seta() {
Random rd=new Random();
try{
PrintWriter out=new PrintWriter(dir);
for(int i=0;i<=4;i++) {
out.println(rd.nextInt(9));
}
out.close();}catch(Exception e) {
System.out.println(e);
}
}
public void js() {
File myf=new File(dir);
String x;
int xint;
try {
if (myf.exists()){
Scanner in=new Scanner(myf);
while(in.hasNextLine()){
x=in.nextLine();
System.out.println(x);
xint=Integer.parseInt(x);
if(max<xint) { max=xint; }
}
in.close();}
}catch(Exception e){
System.out.println(e); }
}
public static void main(String[] args) {
text1 my=new text1();
my.seta();
my.js();
System.out.println("................");
System.out.println(my.max);
}
}
package demo13;
//实例:输入一个文件名并显示该文件的相关属性
import java.io.*;
public class l3 {
public static void main(String[] args) {
try{
File f=new File("d:\\myfile1.txt");
if(f.exists()){
System.out.println("文件是否可读"+f.canRead());
System.out.println("文件名:"+f.getName());
System.out.println("文件路径"+f.getPath());
System.out.println("文件绝对路径"+f.getAbsolutePath());
System.out.println("文件长度"+f.length());
}else{
System.out.println("文件没有找到");
}
}catch(Exception e){
System.out.print(e);
}
}
}
package demo13;
import java.io.*;
public class l4 {
public void filewrite(String filename) {
try {
FileInputStream fin=new FileInputStream(FileDescriptor.in);
FileOutputStream fout=new FileOutputStream(filename);
System.out.println("以#字号结束");
char ch;
while((ch=(char)fin.read())!='#'){
fout.write(ch);
}
fin.close();
fout.close(); }catch(Exception e){
System.out.print(e);
}
}
public void fileread(String filename) {
try {
FileInputStream fin=new FileInputStream(filename);
FileOutputStream fout=new FileOutputStream(FileDescriptor.out);
while(fin.available()>0){
int data=fin.read();
fout.write(data);
}
fin.close();
fout.close(); }catch(Exception e) {
System.out.print(e);
}
}
public static void main(String[] args) {
l4 my=new l4();
my.filewrite("d:\\myfile.txt");
my.fileread("d:\\myfile.txt");
}
}
package demo13;
import java.io.*;
import java.util.*;
public class l6 {
public static void main(String[] args) {
String name="有趣而简单的写操作";
String name1="即文本的输出";
double x=3445.4;
String dir="d:\\myfile1.txt";
String list;
//--------------------------------------------------------------------------------------------------------
try{
PrintWriter out=new PrintWriter(dir);
out.print(name+'\n');
out.println(name1);
out.println(" *************** ");
out.println(x);
out.close();
File my1=new File(dir);
if (my1.exists()){
Scanner in=new Scanner(my1);
while(in.hasNextLine()){
System.out.println(in.nextLine());
}
in.close(); }
}
catch(Exception e){
System.out.println(e); }
}
}
package demo13;
import java.io.*;
public class l5 {
public void fileread(String s) {
String thisline;
int count=0;
try{
FileReader fr=new FileReader(s);
BufferedReader bfr=new BufferedReader(fr);
while((thisline=bfr.readLine())!=null){
count++;
System.out.println(thisline);
}
System.out.println("共读取了"+count+"行");
bfr.close();
}catch(IOException e){
System.out.println(e);
}
}
public void filewrite(String sn){
String str=new String();
try{
String s="hello \n java!";
BufferedWriter out=new BufferedWriter(new FileWriter(sn));
out.write(s);
out.close();
}catch(IOException e){
System.out.println(e);
}
}
public static void main(String[] args) {
l5 my=new l5();
my.fileread("d:\\myfile.txt");
my.filewrite("d:\\myfile1.txt");
my.fileread("d:\\myfile1.txt");
} }