package com.tuzi.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestFile {
public TestFile() {
}
public static String read(String path) {
StringBuffer res = new StringBuffer();
String line = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(path), "UTF-8"));
while ((line = reader.readLine()) != null) {
res.append(line + "\n");
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res.toString();
}
// sum += 1;
// if(sum == start){
// res.append(line + "\n");
// if(y < size){
//
// }
// }
public static String readFor(String path,int num,int size) {
StringBuffer res = new StringBuffer();
String line = null;
int sum = 0;
int start = (num-1) * size + 1;
int y =0;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(path), "UTF-8"));
while ((line = reader.readLine()) != null) {
sum += 1;
if(sum == start){
sum -= 1;
if(y < size){
res.append(line + ",");
y+=1;
}else{
break;
}
}
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res.toString();
}
public static boolean write(String cont, File dist) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(dist));
writer.write(cont);
writer.flush();
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
File src = new File("C:/Users/hao/Desktop/config.php");
String cont = TestFile.read("C:/Users/hao/Desktop/config.php");
String key = TestFile.readFor("C:/Users/hao/Desktop/key.txt",1,5);
//远程地址
String keyyuancheng = TestFile.readFor("http://23.244.152.138/C:/11.txt",1,5);
String keys = key.substring(0, key.length()-1);
System.out.println(keyyuancheng);
//对得到的内容进行处理
// cont = cont.replaceAll("苹果CMS", keys);
// System.out.println(cont);
//更新源文件
// System.out.println(TestFile.write(cont, src));
}
}