
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.FileReader
import java.io.IOException
import java.io.PrintWriter
import java.util.Scannerpublic class AddList {
private String filePath = ""
private String bakPath = ""
private String content = ""
Scanner sc = new Scanner(System.in)
public String readFile(){
content = ""
if (isNull(filePath)) {
filePath = sc.nextLine()
}
File file = new File(filePath)
FileReader fr = null
try {
if (file.exists()) {
fr = new FileReader(file)
char[] chars = new char[1024]
int n = 0
while((n = fr.read(chars)) != -1){
String string = new String(chars, 0, n)
content = content + string
}
} else {
System.out.println("文件不存在")
}
} catch (Exception e) {
e.printStackTrace()
} finally {
if (fr != null) {
try {
fr.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
return content
}
public void writeFile(String path){
File file = new File(path)
FileOutputStream fos = null
mkDirs(path)
try {
fos = new FileOutputStream(file)
BufferedOutputStream bos = new BufferedOutputStream(fos)
PrintWriter pw = new PrintWriter(bos, true)
pw.print(content)
pw.flush()
} catch (FileNotFoundException e) {
e.printStackTrace()
} finally {
if (fos != null) {
try {
fos.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
}
public void writeFile(){
if (isNull(filePath)) {
System.out.println("文件存储路径:")
filePath = sc.nextLine()
}
File file = new File(filePath)
FileOutputStream fos = null
mkDirs(filePath)
try {
fos = new FileOutputStream(file)
BufferedOutputStream bos = new BufferedOutputStream(fos)
PrintWriter pw = new PrintWriter(bos, true)
pw.print(content)
pw.flush()
} catch (FileNotFoundException e) {
e.printStackTrace()
} finally {
if (fos != null) {
try {
fos.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
}
public void mkDirs(String filepath){
if (filepath.indexOf("\\") != -1) {
filepath = filepath.replaceAll("\\", "/")
}
int n = filepath.indexOf("//")
String path = filepath.substring(0, n) + "//"
filepath = filepath.substring(filepath.indexOf("//") + 1, filepath.length())
String[] files = filepath.split("/")
for (int i = 0i <files.length - 1i++) {
path = path + files[i]
File file = new File(path)
if (!file.exists()) {
file.mkdir()
}
}
}
public void addImfor(){
System.out.println("--------增加记录---------")
String name = ""
String tel = ""
String email = ""
content = readFile()
while(true){
System.out.println("姓名:")
name = sc.next()
System.out.println("电话:")
tel = sc.next()
System.out.println("Email:")
email = sc.next()
content = content + name + "<>" + tel + "<>" + email +"<==>"
System.out.println("0、Exit 1、继续")
int i = sc.nextInt()
if (i == 0) {
break
}
}
writeFile()
}
public void deleteImfor(){
System.out.println("---------删除记录---------")
String name = ""
String[] imfors = null
content = readFile()
while(true){
System.out.println("你要删除的姓名是:")
name = sc.next()
if (content.indexOf(name) != -1) {
imfors = content.split("<==>")
for (int i = 0i <imfors.lengthi++) {
if (imfors[i].indexOf(name) != -1) {
imfors[i] = ""
}
}
content = ""
for (int i = 0i <imfors.lengthi++) {
if (!isNull(imfors[i])) {
content = content + imfors[i] + "<==>"
}
}
writeFile()
System.out.println("删除成功")
} else {
System.out.println("此人不存在")
}
System.out.println("0、Exit 1、继续")
int i = sc.nextInt()
if (i == 0) {
break
}
}
}
public void viewAll(){
System.out.println("----------显示所有------------")
content = readFile()
if (!isNull(content)) {
String[] imfors = content.split("<==>")
System.out.println("姓名\t电话\tEmail")
for (int i = 0i <imfors.lengthi++) {
String[] imfor = imfors[i].split("<>")
for (int j = 0j <imfor.lengthj++) {
System.out.print(imfor[j] + "\t")
}
System.out.println()
}
} else {
System.out.println("暂时还没有记录")
}
}
public void queryImfor(){
System.out.println("----------查找记录-----------")
content = readFile()
if (!isNull(content)) {
String result = ""
String[] imfors = null
String[] imfor = null
String name = ""
boolean bool = false
while(true){
result = ""
System.out.println("请输入关键字(按姓名查找):")
name = sc.next()
bool = false
if (content.indexOf(name) != -1) {
imfors = content.split("<==>")
for (int i = 0i <imfors.lengthi++) {
if (imfors[i].indexOf(name) != -1) {
imfor = imfors[i].split("<>")
if (imfor[0].equals(name)) {
bool = true
result = result + imfors[i] + "<==>"
}
}
}
if (bool) {
imfors = result.split("<==>")
System.out.println("姓名\t电话\tEmail")
for (int i = 0i <imfors.lengthi++) {
imfor = imfors[i].split("<>")
for (int j = 0j <imfor.lengthj++) {
System.out.print(imfor[j] + "\t")
}
System.out.println()
}
} else {
System.out.println("无此人信息")
}
} else {
System.out.println("无此人信息")
}
System.out.println("0、Exit 1、继续")
int i = sc.nextInt()
if (i == 0) {
break
}
}
} else {
System.out.println("文件还没有记录")
}
}
public void copy(){
System.out.println("----------备份-----------")
content = readFile()
if (isNull(bakPath)) {
System.out.println("备份全路径:")
bakPath = sc.next()
}
writeFile(bakPath)
System.out.println("备份成功")
}
public boolean isNull(String string){
if (null == string || "" == string || 0 == string.length()) {
return true
} else {
return false
}
}
public static void main(String[] args) {
AddList add = new AddList()
Scanner sc = new Scanner(System.in)
int operater = 0
while(true){
System.out.println("选择功能:\n1、增加记录 2、删除记录 3、显示所有 4、查询记录 5、备份 6、退出")
operater = sc.nextInt()
if (1 == operater) {
add.addImfor()
} else if (2 == operater) {
add.deleteImfor()
} else if (3 == operater) {
add.viewAll()
} else if (4 == operater) {
add.queryImfor()
} else if (5 == operater) {
add.copy()
} else if (6 == operater) {
System.out.println("谢谢使用")
break
}
}
}
}
在请求头里设置Range,可以拿到不同的部分,前提还需要web server支持。
/**
* 开始下载
* @throws Exception
*/
public void startDown() throws Exception{
HttpClient httpClient = new DefaultHttpClient()
try {
//获取下载文件信息
getDownloadFileInfo(httpClient)
//启动多个下载线程
startDownloadThread()
//开始监视下载数据
monitor()
} catch (Exception e) {
throw e
} finally {
httpClient.getConnectionManager().shutdown()
}
}
/**
* 获取下载文件信息
*/
private void getDownloadFileInfo(HttpClient httpClient) throws IOException,
ClientProtocolException, Exception {
HttpHead httpHead = new HttpHead(url)
HttpResponse response = httpClient.execute(httpHead)
//获取HTTP状态码
int statusCode = response.getStatusLine().getStatusCode()
if(statusCode != 200) throw new Exception("资源不存在!")
if(getDebug()){
for(Header header : response.getAllHeaders()){
System.out.println(header.getName()+":"+header.getValue())
}
}
//Content-Length
Header[] headers = response.getHeaders("Content-Length")
if(headers.length > 0)
contentLength = Long.valueOf(headers[0].getValue())
httpHead.abort()
httpHead = new HttpHead(url)
httpHead.addHeader("Range", "bytes=0-"+(contentLength-1))
response = httpClient.execute(httpHead)
if(response.getStatusLine().getStatusCode() == 206){
acceptRanges = true
}
httpHead.abort()
}
/**
* 启动多个下载线程
* @throws IOException
* @throws FileNotFoundException
*/
private void startDownloadThread() throws IOException,
FileNotFoundException {
//创建下载文件
File file = new File(localPath)
file.createNewFile()
RandomAccessFile raf = new RandomAccessFile(file, "rw")
raf.setLength(contentLength)
raf.close()
//定义下载线程事件实现类
DownloadThreadListener listener = new DownloadThreadListener() {
public void afterPerDown(DownloadThreadEvent event) {
//下载完一个片段后追加已下载字节数
synchronized (object) {
DownloadTask.this.receivedCount += event.getCount()
}
}
public void downCompleted(DownloadThreadEvent event) {
//下载线程执行完毕后从主任务中移除
threads.remove(event.getTarget())
if(getDebug()){
System.out.println("剩余线程数:"+threads.size())
}
}
}
//不支持多线程下载时
if (!acceptRanges) {
if(getDebug()){
System.out.println("该地址不支持多线程下载")
}
//定义普通下载
DownloadThread thread = new DownloadThread(url, 0, contentLength, file, false)
thread.addDownloadListener(listener)
thread.start()
threads.add(thread)
return
}
//每个请求的大小
long perThreadLength = contentLength / threadCount + 1
long startPosition = 0
long endPosition = perThreadLength
//循环创建多个下载线程
do{
if(endPosition >= contentLength)
endPosition = contentLength - 1
DownloadThread thread = new DownloadThread(url, startPosition, endPosition, file)
thread.addDownloadListener(listener)
thread.start()
threads.add(thread)
startPosition = endPosition + 1//此处加 1,从结束位置的下一个地方开始请求
endPosition += perThreadLength
} while (startPosition < contentLength)
}
fin.open("c:\\Documents and Settings\\1.txt")全路径+文件名
不过注意在微软WINDOWS系统中目录分隔符用\\
而LINUX/UNIX目录分隔符用/
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)