@Sarah
2015-06-22T14:14:34.000000Z
字数 6675
阅读 1041
//上堂课我的作业更正版本
package file;
import java.io.*;
public class file {
public static void main(String[] args) throws Exception {
{
try {
FileWriter fw = new FileWriter("File.txt");
InputStreamReader x = new InputStreamReader(System.in);
BufferedReader y = new BufferedReader(x);
int i=1;
for (i++) {
System.out.print(i+":");
String s = y.readLine();
if (s.equals("[end]")) {
fw.close();
break;
} else {
fw.write(s+"\n");
}
}
} catch (Exception e) {
}
}
}
}
可以同时写入和读取文件 rw
可以显示文件最后一次修改的时间
可以把这个时间格式化成年月日格式
可以用f.mkdirs新建一组文件夹“\a\b\c\d”
可以从第几个开始读取 遍历之后的内容
FileNameFilter()
//读取abc.txt里第几个字符
import java.io.*;
public class RandomAccess {
public static void main(String[] args) {
try {
RandomAccessFile af=new RandomAccessFile("abc.txt", "rw");
//rw可以读取和写入
for(int i=0;i<256;i++)
af.write(i);
af.seek(0);//从第0个开始
int s=af.readInt();
System.out.println(s);//第0个是什么?
af.skipBytes(65);//第65个? E
System.out.println((char)af.readByte());
af.skipBytes(-5);//输出的是第67个F
System.out.println((char)af.readByte());
af.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.*;
public class DisplayFile {
public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length<1){
System.out.println("Usage:JAVA DisplayFile <filename> [-l length] [-t]");
System.out.println("\t-l:length of file contents to display.");
System.out.println("\t-t:display content with text mode.");
System.exit(-1);
}
String filename=args[0];
boolean hex=true;
int length=256;
for(int i=1;i<args.length;i++){
if(args[i].toLowerCase().equals("-l")){
if(args.length<i+2){
System.out.println("please tell me length of file content.");
System.exit(-2);
}
else
try{
length=Integer.parseInt(args[i+1]);
}
catch(NumberFormatException e){
System.out.println(e);
System.exit(-3);
}
i++;
continue;
}
if(args[i].toLowerCase().equals("-t"))
hex=false;
}
byte[] content=new byte[length];
try {
FileInputStream fis=new FileInputStream(filename);
length=fis.read(content);
if(hex)
displayHex(content,length);
else
displayText(content,length);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void displayText(byte[] c,int length){
System.out.print(new String(c,0,length));
}
private static void displayHex(byte[] c,int length){
int i=0;
System.out.print(" Offset");
for(int j=0;j<5;j++)
System.out.print(" ");
System.out.print("Content(hex. format)");
for(int j=0;j<6;j++)
System.out.print(" ");
System.out.print("Content(text format)\n");
for(int j=0;j<80;j++)
System.out.print("-");
System.out.println();
while(i<length){
int j=0,k=0;
System.out.printf("%08X ", i);
while(j<16){
System.out.printf("%02X ", c[i+j]);
j++;
if(i+j==length)
break;
}
if(j<16)
for(k=0;k<16-j;k++)
System.out.print(" ");
k=0;
System.out.print(" ");
while(k<j){
System.out.print((char)(c[i+k]>=32 && c[i+k]<=126?c[i+k]:'.'));
k++;
}
System.out.println();
i+=16;
}
}
}
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
class FileFilterTest{
public static void main(String args[]){
File dir=new File("./");
Filter filter=new Filter(".class");
String files[]=dir.list();
for(int i=0;i<files.length;i++){
File f=new File(files[i]);
Date d=new Date(f.lastModified());
String df="yyyy-MM-dd HH:mm:ss";
String sdf=new SimpleDateFormat(df).format(d);
System.out.print(sdf+"\t");
if (f.isFile())
System.out.print("\t"+f.length()+"\t");
else
System.out.print("<DIR>\t\t");
System.out.print(f.getName()+"\n");
}
}
}
class Filter implements FilenameFilter{
String extent;
Filter(String extent){
this.extent=extent;
}
public boolean accept(File dir,String name){
return name.endsWith(extent);
}
}
(.)表示当前文件夹
mf.accept(File,String)
可以在制定文件夹里寻找后缀/文件夹名字是指定字符的文件,比如找.txt文件,是true的话就留着,是false就从遍历里去掉,然后返回符合的几个文件名字,
list(FilenameFilter m)
m.accept(s[i])
m.accept
//输出文件最后修改的日期并shimiaofeng格式化为年月日十秒分格式
import java.io.*;
import java.text.SimpleDateFormat;
public class FIleClassTest {
public static void main(String[] args) {
try{
File f=new File("./");
if(f.exists()){
if(f.isFile()){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s=sdf.format(f.lastModified());
System.out.println(s);
// FileReader fr=new FileReader(f);
}else{
MyFilter mf=new MyFilter();
String files[]=f.list(mf);
for(String s:files)
System.out.println(s);
}
}else{
}
}
catch(Exception e){
System.out.println(e);
}
}
}
//为了实现FilenameFilter建立的接口
import java.io.File;
import java.io.FilenameFilter;
public class MyFilter implements FilenameFilter {
@Override
public boolean accept(File arg0, String arg1) {
// TODO Auto-generated method stub
if(arg1.indexOf("a")>=0)
return true;
else
return false;
}
}
//新写法
for (int a : arr)
System.out.println(a);
//旧写法
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
就是输出一排特别整齐的东西在terminal里那个!能显示出文件里的内容
try catch可以自动写 选中要try catch的代码 右键
序列化:我们可以通过序列化来保存一个对象的状态(实例变量)到文件中,也可以从这个格式化的文件中很容易地读取对象的状态从而可以恢复我们保存的对象。
用来实现序列化的类都在java.io包中,我们常用的类或接口有:
ObjectOutputStream:提供序列化对象并把其写入流的方法
ObjectInputStream:读取流并反序列化对象
Serializable:一个对象想要被序列化,那么它的类就要实现 此接口
package Circle;
import java.io.Serializable;
public class Point implements Serializable{
int x,y;
private Point(){
this(0,0);
}
public Point(int bb){
}
public Point(int x,int y){
this.x=x;
this.y=y;
}
Point getPoint(){
Point p=new Point();
p.setX(-x);
p.setY(-y);
return p;
}
public void setX(long xx){
x=(int)xx;
}
void setX(int xx){
x=xx;
}
void setY(int yy){
y=yy;
}
Point getApoint(){ //新函数 返回值是point
Point q=new Point(-x,-y);
return q;
}
public double distance(Point p2){ //point是原来的点坐标,p2是另一点的坐标
double d=0;
d=Math.sqrt((x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y));
return d;
}
public boolean equals(Point p3){
if (p3.x==x && p3.y==y) {
return true;
}
else {
return false;
}
}
public String toString(){
return "["+x+","+y+"]";
}
public int getX(){
return x;
}
public int getY() {
return y;
}
}
//创建一个接口
package Circle;
import java.io.*;
public class SerializableTest {
public static void main(String[] args) {
try {
ObjectInputStream oi=new ObjectInputStream(new FileInputStream("point"));
Point p1=(Point)oi.readObject();
System.out.println(p1);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
简单地程序都是一个线程
进程process
多线程的意思就是,程序可以边打字边数日期,两件事互相不干扰,各自进行。
//在两个变量数数的同时读取键盘输入 主函数
import java.io.*;
public class Main {
public static void main(String[] args)throws Exception {
BufferedReader br=new BufferedReader(
new InputStreamReader(System.in)
);
MyThread t=new MyThread();
Thread r=new Thread(t);
r.start();
Thread.sleep(1500);
MyThread t1=new MyThread();
new Thread(t1).start();
while(true){
String s=br.readLine();
System.out.println(s);
}
}
}
//在两个变量数数的同时读取键盘输入 接口的函数
//public class MyThread extends Thread {
public class MyThread extends Object implements Runnable {
//这里是个接口 因为有runnable所以不能写Thread的函数,不然有两个接口了
public void run(){
int i=0;
while(true){
System.out.println("\t"+i);
i++;
if(i==3)
break;//i数数数到3就结束
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
上面谁要用到接口来着 就是什么T没有函数不能直接用 但是必须叫这个名字 用一个函数调用 所以要自己写接口
可以自动构造函数 然后区别的地方调用一下就可以用了
貌似是list
作业
读取六级文档 并输入到一个数组string里面 可以设置范围是一万
输入流/输出流
字节流/字符流
节点流/处理流
以后用的时候:依稀记得有这么一个buffer东西,然后打开API回去查一下。