测试代码:
package com;
import java.io.File;
import java.io.IOException;
public class fileDemo {
public static void main(String[] args) throws IOException {
File f= new File("C:\\Users\\Administrator\\Desktop\\文件的分割与合并.java");
System.out.println("是文件吗? 答案:"+ f.isFile() );
System.out.println("是目录吗? 答案:"+ f.isDirectory() );
System.out.println("是可读? 答案:"+ f.canRead() );
System.out.println("是可写? 答案:"+ f.canWrite() );
System.out.println("文件的长度:"+ f.length() );
System.out.println("文件存在吗? 答案:"+ f.exists() );
System.out.println("返回文件名(不包括路径):"+ f.getName() );
System.out.println("检索指定的字符串 . 的位置:"+ f.getName().indexOf(".") );
System.out.println("截取字符串,从a到b-1:"+ f.getName().substring(0,8) );
System.out.println("返回文件名(不包括路径和后缀名):"+ f.getName().substring(0,f.getName().indexOf(".")) );
System.out.println("获取文件名后缀:"+ f.getName().substring(f.getName().lastIndexOf(".")+1) );
System.out.println("返回相对路径:"+ f.getPath() );
System.out.println("返回绝对路径:"+ f.getAbsolutePath() );
System.out.println("返回父目录:"+ f.getParent() );
System.out.println("把文件对象转化成字符串:"+ f.toString() );
System.out.println("不存在此文件就创建:"+ f.createNewFile() );
}
}
====================================
测试输出是:
是文件吗? 答案:true
是目录吗? 答案:false
是可读? 答案:true
是可写? 答案:true
文件的长度:3666
文件存在吗? 答案:true
返回文件名(不包括路径):文件的分割与合并.java
检索指定的字符串 . 的位置:8
截取字符串,从a到b-1:文件的分割与合并
返回文件名(不包括路径和后缀名):文件的分割与合并
获取文件名后缀:java
返回相对路径:C:\Users\Administrator\Desktop\文件的分割与合并.java
返回绝对路径:C:\Users\Administrator\Desktop\文件的分割与合并.java
返回父目录:C:\Users\Administrator\Desktop
把文件对象转化成字符串:C:\Users\Administrator\Desktop\文件的分割与合并.java
不存在此文件就创建:false
=========================================================
文件内容操作:
package com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class hashMapDemo
{
public static void main(String[] args) throws IOException
{
File f1 = new File("C:\\Users\\Administrator\\Desktop\\文件的分割与合并.java");
File f2 = new File("C:\\Users\\Administrator\\Desktop\\文件的分割与合并-copy.java");
FileInputStream fio = new FileInputStream(f1);
FileOutputStream fout = new FileOutputStream(f2);
int fileSize = fio.available();
System.out.println("总的文件字节数是:" + fileSize);
byte b1 = (byte) fio.read();
System.out.println("第一个字节值:" + b1);
System.out.println("第一个字符:" + (char) b1);
int leftFileSize = fio.available();
System.out.println("剩余的文件字节数是:" + leftFileSize);
System.out.print("连续读取20个字符:");
byte[] b2 = new byte[20];
char[] c2 = new char[20];
for(int i=0; i <20; i++)
{
b2[i] = (byte)fio.read();
c2[i] = (char)b2[i];
System.out.print(c2[i]);
}
System.out.println("");
System.out.println("-----继续读取字符:---------------" );
byte[] b3 = new byte[20];
int a = fio.read(b3);
System.out.println("a的数据是:"+a);
char[] c3 = new char[20];
for(int i=0; i <20; i++)
{
c3[i] = (char)b3[i];
System.out.print(c3[i]);
}
System.out.println("");
leftFileSize = fio.available();
System.out.println("剩余的文件字节数是:" + leftFileSize);
byte[] b4 = new byte[4000];
int a2 = fio.read(b4, 0 ,4000);
System.out.println("a2的数据是:"+a2);
char[] c4 = new char[4000];
for(int i=0; i <3612; i++)
{
c4[i] = (char)b4[i];
System.out.print(c4[i]);
}
System.out.println("");
leftFileSize = fio.available();
System.out.println("剩余的文件字节数是:" + leftFileSize);
fio.close();
fout.close();
}
}
===================================================================================
file 复制内容 达一种方法:
package com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class hashMapDemo
{
public static void main(String[] args) throws IOException
{
File f1 = new File("C:\\Users\\Administrator\\Desktop\\文件的分割与合并.java");
File f2 = new File("C:\\Users\\Administrator\\Desktop\\文件的分割与合并-copy.java");
FileInputStream fio = new FileInputStream(f1);
FileOutputStream fout = new FileOutputStream(f2);
int fileSize = fio.available();
for(int i=0; i <fileSize; i++)
{
fout.write(fio.read());
}
System.out.print("结束");
fio.close();
fout.close();
}
}
----------------------------------------------------------------------
file 复制内容 达 2 种方法:
package com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class hashMapDemo
{
public static void main(String[] args) throws IOException
{
File f1 = new File("C:\\Users\\Administrator\\Desktop\\文件的分割与合并.java");
File f2 = new File("C:\\Users\\Administrator\\Desktop\\文件的分割与合并-copy.java");
FileInputStream fio = new FileInputStream(f1);
FileOutputStream fout = new FileOutputStream(f2);
int fileSize = fio.available();
byte[] b = new byte[1024*1024];
fio.read(b);
fout.write(b);
fileSize = fio.available();
System.out.print("剩余字节数:"+ fileSize);
fio.close();
fout.close();
}
}
========================================
package com;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class hashMapDemo
{
public static void main(String[] args) throws IOException
{
URL cqu = new URL("http://www.baidu.com");
InputStreamReader isr = new InputStreamReader(cqu.openStream());
BufferedReader in = new BufferedReader(isr);
String inputLine;
while((inputLine=in.readLine()) !=null)
{
System.out.println(inputLine);
}
in.close();
}
}
==================================================
socket 基础编程
服务器端代码:
package com;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.*;
public class Server
{
public static void main(String[] args) throws Exception
{
ServerSocket s = new ServerSocket(5600);
Socket sl ;
String sendString = "hello net world";
OutputStream slout;
DataOutputStream dos;
while (true)
{
try
{
sl = s.accept();
slout = sl.getOutputStream();
dos = new DataOutputStream(slout);
dos.writeUTF(sendString);
slout.close();
sl.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
----------------------------
-客户端
package com;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client
{
public static void main(String[] args) throws Exception, IOException
{
int c;
Socket sl;
InputStream slIn;
DataInputStream dis;
sl = new Socket("localhost" , 5600);
slIn = sl.getInputStream();
dis = new DataInputStream(slIn);
String st = new String(dis.readUTF());
System.out.println(st);
slIn.close();
sl.close();
}
}
====================================
基于多线程的socket通信:
服务器端代码:
package com;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.*;
public class Server
{
public static void main(String[] args) throws Exception
{
try
{
System.out.println("服务器端正在等待连接:");
ServerSocket serverSocket = new ServerSocket(5500);
Socket s= null;
while (true)
{
s = serverSocket.accept();
new ServerThread(s);
}
}catch (Exception e)
{
e.printStackTrace();
}
}
}
-----------------------
package com;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ServerThread extends Thread
{
private Socket s;
private DataInputStream is ;
private DataOutputStream os ;
public ServerThread(Socket socket) throws IOException{
super();
s= socket;
is = new DataInputStream( s.getInputStream());
os = new DataOutputStream( s.getOutputStream());
start();
}
public void run()
{
try{
String str;
double result , zhi ;
boolean NotEnd = true ;
while(NotEnd)
{
str = is.readUTF();
if(!str.equals("bye"))
{
zhi = Double.parseDouble(str);
System.out.println("接收到的值是:"+ zhi);
result = zhi * zhi;
str = Double.toString(result);
os.writeUTF(str);
os.flush();
System.out.println("平方值"+ str +" sending");
}else{
NotEnd =false;
os.writeUTF("bye");
os.flush();
}
}
is.close();
os.close();
s.close();
}catch (Exception e)
{
e.printStackTrace();
}
}
}
------------------------------------
客户端代码:
package com;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client
{
public static void main(String[] args) throws Exception, IOException
{
try
{
int c;
Socket s;
InputStream slIn;
DataInputStream dis;
s = new Socket("localhost", 5500);
DataInputStream is = new DataInputStream(s.getInputStream());
DataOutputStream os = new DataOutputStream(s.getOutputStream());
System.out.println("输入你要计算的平方值,输入bye结束:");
String outStr, inStr;
boolean NotEnd = true;
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
while (NotEnd)
{
outStr = buf.readLine();
os.writeUTF(outStr);
os.flush();
inStr = is.readUTF();
if (!inStr.equals("bye"))
System.out.println("来自于服务器端计算后的结果是:" + inStr);
else
NotEnd = false;
}
is.close();
os.close();
s.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
=========================================
输出结果:
服务器端正在等待连接:
接收到的值是:4.0
平方值16.0 sending
接收到的值是:5.0
平方值25.0 sending
--------------------------
输入你要计算的平方值,输入bye结束:
4
来自于服务器端计算后的结果是:16.0
5
来自于服务器端计算后的结果是:25.0
=========================================
线程池 基础编程
服务器端代码:以下是转载文章
http://blog.csdn.net/semillon/article/details/7515926 线程
http://www.cnblogs.com/rond/p/3565113.html
http://www.cnblogs.com/wihainan/p/4765862.html 线程池
http://www.cnblogs.com/absfree/p/5357118.html
==============================================
文件上传:
http://www.jb51.net/article/45170.htm
http://www.cnblogs.com/friends-wf/p/3766374.html
http://www.cnblogs.com/xiaoluo501395377/archive/2012/10/26/2740882.html