java文件相关
import java.io.*;
import java.util.Scanner;
import java.io.IOException;
public class Try_Wenjian{
public static void main(String[] args) throws IOException{
int size;
FileInputStream f = new FileInputStream("/home/yijiull/javacode/in.txt");
FileOutputStream fout = new FileOutputStream ("/home/yijiull/javacode/copy.txt");
BufferedInputStream bis = new BufferedInputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(fout);
System.out.println("Total Available Bytes: " + (size = f.available()));;
int n =size / 3;
System.out.println("First " + n + " bytes of the file one read() at a time");
for(int i = 0; i < n; i++){
fout.write(f.read());
}
System.out.println("Still available " + f.available());
System.out.println("Reading the next " + n + " bytes with one read(b[])");
byte b[] = new byte[n];
if(f.read(b) != n) {
System.out.println("Could not read " + n + " bytes");
}
fout.write(b);
System.out.println("Still Available " + f.available());
System.out.println("Reading the rest bytes with read(b, offset, len)");
int count = 0;
while((count = f.read(b, 0, n)) != -1){
fout.write(b, 0, count);
}
System.out.println("Still Available " + f.available());
f.close();
fout.flush();
fout.close();
String filename = "temp.txt";
RandomAccessFile raf = null;
String s1 = "Happy double 11 day!";
String s2 = "双十一快乐!";
long length, pos;
try{
raf = new RandomAccessFile(filename, "rw");
raf.writeChars(s1);
pos = raf.getFilePointer();
length = s1.length();
System.out.println("第一个字符串的长度: " + length);
System.out.println("写入第一个字符串后,文件指针: " + pos);
pos = raf.getFilePointer();
raf.writeChars(s2);
raf.seek(pos);
for(int i = 0; i < s2.length(); i++){
System.out.print(raf.readChar());
}
System.out.println();
pos = raf.getFilePointer();
System.out.println("文件写入" + s2.length() + "个字符后,文件指针: " + pos);
}catch(FileNotFoundException ex){
System.out.println("文件不存在");
}catch(IOException ex){
}
Scanner s = null;
PrintWriter pw = null;
try{
s = new Scanner(new File("date.txt"));
pw = new PrintWriter("dest.txt");
while(s.hasNextLine()){
String str = s.nextLine();
int sum = getLinrSum(str);
pw.println(str + '\t' + sum);
}
}catch(FileNotFoundException e){
e.printStackTrace();
}finally{
if(s != null){
s.close();
}
if(pw != null){
pw.close();
}
}
}
private static int getLinrSum(String str){
Scanner cin = new Scanner(str);
int sum = 0;
while(cin.hasNextInt()){
sum += cin.nextInt();
}
return sum;
}
}
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
public class try_file{
public static void main(String args[]){
File dir = new File("/media/yijiull/DATA/acm");
System.out.println("列出目录" + dir + "中的pdf文件");
Filter filter = new Filter("pdf");
File fs1[] = dir.listFiles(filter);
display(fs1);
System.out.println("列出" + dir + "中的所有子目录");
DirFilter filter2 = new DirFilter();
File fs2[] = dir.listFiles(filter2);
display(fs2);
File f = new File(fs2[0].getPath() + "_newDir");
System.out.println(f.mkdir());
File fs3[] = dir.listFiles(filter2);
display(fs3);
}
public static void display(File[] fs){
for(int i = 0 ; i < fs.length; i++){
if(fs[i].isDirectory()){
System.out.println("目录 " + fs[i]);
}else {
System.out.println("文件 " + fs[i]);
}
}
}
}
class Filter implements FilenameFilter{
String extent;
Filter(String extent){
this.extent = extent;
}
public boolean accept(File die, String name){
return name.endsWith("." + extent);
}
}
class DirFilter implements FileFilter{
public boolean accept(File path){
return path.isDirectory();
}
}