失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 《Java就业培训教程》_张孝祥_书内源码_07

《Java就业培训教程》_张孝祥_书内源码_07

时间:2021-11-28 23:23:03

相关推荐

《Java就业培训教程》_张孝祥_书内源码_07

《Java就业培训教程》 作者:张孝祥 书中源码

《Java就业培训教程》P239源码

程序清单:FileTest.java

import java.io.*;

public class FileTest

{

public static void main(String[] args)

{

File f=new File("c://1.txt");

if(f.exists())

f.delete();

else

try

{

f.createNewFile();

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

System.out.println("File name:"+f.getName());

System.out.println("File path:"+f.getPath());

System.out.println("Abs path:"+f.getAbsolutePath());

System.out.println("Parent:"+f.getParent());

System.out.println(f.exists()?"exists":"does not exist");

System.out.println(f.canWrite()?"is writeable":"is not writeable");

System.out.println(f.canRead()?"is readable":"is not readable");

System.out.println(f.isDirectory()?"is ":"is not"+" a directory");

System.out.println(f.isFile()?"is normal file":"might be a named pipe");

System.out.println(f.isAbsolute()?"is absolute":"is not absolute");

System.out.println("File last modified:"+f.lastModified());

System.out.println("File size:"+f.length()+" Bytes");

}

}

《Java就业培训教程》P242源码

程序清单:RandomFileTest.java

import java.io.*;

public class RandomFileTest

{

public static void main(String [] args) throws Exception

{

Employee e1 = new Employee("zhangsan",23);

Employee e2 = new Employee("Lisi",24);

Employee e3 = new Employee("Wangwu",25);

RandomAccessFile ra=new RandomAccessFile("c://1.txt","rw");

ra.write(e1.name.getBytes());

ra.writeInt(e1.age);

ra.write(e2.name.getBytes());

ra.writeInt(e2.age);

ra.write(e3.name.getBytes());

ra.writeInt(e3.age);

ra.close();

RandomAccessFile raf=new RandomAccessFile("c://1.txt","r");

int len=8;

raf.skipBytes(12); //跳过第一个员工的信息,其中姓名8字节,年龄4字节

System.out.println("第二个员工信息:");

String str="";

for(int i=0;i<len;i++)

str=str+(char)raf.readByte();

System.out.println("name:"+str);

System.out.println("age:"+raf.readInt());

System.out.println("第一个员工的信息:");

raf.seek(0); //将文件指针移动到文件开始位置

str="";

for(int i=0;i<len;i++)

str=str+(char)raf.readByte();

System.out.println("name:"+str);

System.out.println("age:"+raf.readInt());

System.out.println("第三个员工的信息:");

raf.skipBytes(12); //跳过第二个员工信息

str="";

for(int i=0;i<len;i++)

str=str+(char)raf.readByte();

System.out.println("name:"+str.trim());

System.out.println("age:"+raf.readInt());

raf.close();

}

}

class Employee

{

String name;

int age;

final static int LEN=8;

public Employee(String name,int age)

{

if(name.length()>LEN)

{

name = name.substring(0,8);

}

else

{

while(name.length()<LEN)

name=name+"/u0000";

}

this.name=name;

this.age=age;

}

}

《Java就业培训教程》P247源码

程序清单:FileStream.java

import java.io.*;

public class FileStream

{

public static void main(String[] args)

{

File f = new File("hello.txt");

try

{

FileOutputStream out = new FileOutputStream(f);

byte buf[]="".getBytes();

out.write(buf);

out.close();

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

try

{

FileInputStream in = new FileInputStream(f);

byte [] buf = new byte[1024];

int len = in.read(buf);

System.out.println(new String(buf,0,len));

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

}

《Java就业培训教程》P250源码

程序清单:PipeStreamTest.java

import java.io.*;

public class PipeStreamTest

{

public static void main(String args[])

{

try

{

Sender t1=new Sender();

Receiver t2=new Receiver();

PipedOutputStream out = t1.getOutputStream();

PipedInputStream in = t2.getInputStream();

out.connect(in);

t1.start();

t2.start();

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

}

class Sender extends Thread

{

private PipedOutputStream out=new PipedOutputStream();

public PipedOutputStream getOutputStream()

{

return out;

}

public void run()

{

String s=new String("hello,receiver,how are you!");

try

{

out.write(s.getBytes());

out.close();

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

}

class Receiver extends Thread

{

private PipedInputStream in=new PipedInputStream();

public PipedInputStream getInputStream()

{

return in;

}

public void run()

{

String s=null;

byte [] buf = new byte[1024];

try

{

int len =in.read(buf);

s = new String(buf,0,len);

System.out.println(

"the following message comes from sender:/n"+s);

in.close();

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

}

《Java就业培训教程》P253源码

程序清单:ByteArrayTest.java

import java.io.*;

public class ByteArrayTest

{

public static void main(String[] args) throws Exception

{

String tmp="abcdefghijklmnopqrstuvwxyz";

byte [] src =tmp.getBytes();//src为转换前的内存块

ByteArrayInputStream input = new ByteArrayInputStream(src);

ByteArrayOutputStream output = new ByteArrayOutputStream();

new ByteArrayTest().transform(input,output);

byte [] result = output.toByteArray();//result为转换后的内存块

System.out.println(new String(result));

}

public void transform(InputStream in,OutputStream out)

{

int c=0;

try

{

while((c=in.read())!=-1)//read在读到流的结尾处返回-1

{

int C = (int)Character.toUpperCase((char)c);

out.write(C);

}

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

《Java就业培训教程》P263源码

程序清单:Serializatioan.java

import java.io.*;

public class serialization

{

public static void main(String args[])

throws IOException,ClassNotFoundException

{

Student stu=new Student(19,"dintdding",50,"huaxue");

FileOutputStream fos=new FileOutputStream("mytext.txt");

ObjectOutputStream os=new ObjectOutputStream(fos);

try

{

os.writeObject(stu);

os.close();

}catch(IOException e)

{

System.out.println(e.getMessage());

}

stu=null;

FileInputStream fi=new FileInputStream("mytext.txt");

ObjectInputStream si=new ObjectInputStream(fi);

try

{

stu=(Student)si.readObject();

si.close();

}catch(IOException e)

{

System.out.println(e.getMessage());

}

System.out.println("ID is:"+stu.id);

System.out.println("name is:"+stu.name);

System.out.println("age is:"+stu.age);

System.out.println("department is:"+stu.department);

}

}

class Student implements Serializable

{

int id;

String name;

int age;

String department;

public Student(int id,String name,int age,String department)

{

this.id=id;

this.name=name;

this.age=age;

this.department=department;

}

}

《Java就业培训教程》P275源码

import java.io.*;

public class CharDecoder

{

public static void main(String [] args) throws Exception

{

System.getProperties().put("file.encoding","iso8859-1");

System.out.println("please enter a Chinese String");

byte [] buf=new byte[1024];

int ch=0;

int pos=0;

String strInfo=null;

while(true)

{

ch =System.in.read();

System.out.println(Integer.toHexString(ch));

switch(ch)

{

case '/r':

break;

case '/n':

strInfo= new String(buf,0,pos);

for(int i=0;i<strInfo.length();i++)

{

System.out.println(Integer.toHexString

((int)strInfo.charAt(i)));

}

System.out.println(strInfo);

for(int i=0;i<pos;i++)

System.out.write(buf[i]);

System.out.println();//想想为什么要这一句

return;

default:

buf[pos++]=(byte)ch;

}

}

}

}

《Java就业培训教程》P282源码

程序清单: TestInOut.java

import java.io.*;

public class TestInOut implements Runnable

{

Process p=null;

public TestInOut()

{

try

{

p=Runtime.getRuntime().exec("java MyTest");

new Thread(this).start();

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

public void send()

{

try

{

OutputStream ops=p.getOutputStream();

while(true)

{

ops.write("help/r/n".getBytes());

}

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

public static void main(String [] args)

{

TestInOut tio=new TestInOut ();

tio.send();

}

public void run()

{

try

{

InputStream in = p.getInputStream();

BufferedReader bfr=new BufferedReader(

new InputStreamReader(in));

while(true)

{

String strLine=bfr.readLine();

System.out.println(strLine);

}

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

}

class MyTest

{

public static void main(String [] args) throws IOException

{

while(true)

{

System.out.println("hi:"+

new BufferedReader(new InputStreamReader(System.in)).readLine());

}

}

}

如果觉得《《Java就业培训教程》_张孝祥_书内源码_07》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。