读文件
public static void main(String[] args) throws IOException {
FileReader fr=new FileReader("a.txt", Charset.forName("GBK"));
int ch;
while((ch=fr.read())!=-1){
System.out.println((char)ch);
}
fr.close();
}
字节流读取数据
BufferedReader br=new BufferedReader(new InputStreamReader( new FileInputStream("a.txt")));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
解压文件
public class Main {
public static void main(String[] args) throws IOException {
File src=new File("D:\\a.txt");
File dest=new File("D:\\");
unzip(src,dest);
}
private static void unzip(File src, File dest) throws IOException {
ZipInputStream zip=new ZipInputStream(new FileInputStream(src));
ZipEntry entry;
while((entry=zip.getNextEntry())!=null){
System.out.println(entry);
if(entry.isDirectory()){
File file=new File(dest,entry.toString());
file.mkdirs();
}else{
FileOutputStream fos=new FileOutputStream(new File(dest,entry.toString()));
int b;
while((b=zip.read())!=-1){
fos.write(b);
}
fos.close();
zip.closeEntry();
}
}
zip.close();
}
}
压缩文件
private static void tozip(File src, File dest) throws IOException {
ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(new File(dest,"a.zip")));
ZipEntry entry =new ZipEntry("a.txt");
zos.putNextEntry(entry);
FileInputStream fis=new FileInputStream(src);
int b;
while((b=fis.read())!=-1){
zos.write(b);
}
zos.closeEntry();
zos.close();
}
压缩 包括子文件
public static void tozip(File src,ZipOutputStream zos,String name) throws IOException{
File files[]=src.listFiles();
for(File file:files){
if(file.isFile()){
ZipEntry entry=new ZipEntry(name+"\\"+file.getName());
zos.putNextEntry(entry);
FileInputStream fis=new FileInputStream(file);
int b;
while((b=fis.read())!=-1){
zos.write(b);
}
fis.close();
zos.closeEntry();
}else{
tozip(file,zos,name+"\\"+file.getName());
}
}
}
爬取网址// 简单的爬取//这里正则就显得尤为重要
public class Main {
public static void main(String[] args) throws IOException {
String fnet="http:....";//wnagzhi
String fnet1=webCrawler(fnet);
ArrayList<String> fnet2=getData(fnet1,"(.{4})(,|.)",1);//此处为正则表达式,有点忘了
System.out.println(fnet2);
}
private static ArrayList<String> getData(String fnet1, String regex, int index) {
ArrayList<String> list=new ArrayList<>();
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(fnet1);
while(matcher.find()){
list.add(matcher.group(index));
}
return list;
}
private static String webCrawler(String fnet) throws IOException {
StringBuilder sb=new StringBuilder();
URL url=new URL(fnet);
URLConnection conn= url.openConnection();
InputStreamReader isr=new InputStreamReader(conn.getInputStream());
int ch;
while((ch= isr.read())!=-1){
sb.append((char) ch);
}
isr.close();
return sb.toString();
}
}
并发:同一时刻,多个指令在单个cpu进行
并行:同一时刻,多指令多cpu;