//获取源代码:根据给定URL获取HTML,存为txt文件privatevoidgetHtml(Stringfiledir,StringdownloadURL){try{byte[]buffers=newbyte[102400];intsize=buffers.length;URLurl=newURL(d...

//获取源代码:根据给定URL获取HTML,存为 txt文件

private void getHtml(String filedir,String downloadURL){

try {

byte[] buffers=new byte[102400];

int size=buffers.length;

URL url=new URL(downloadURL);

HttpURLConnection conn=(HttpURLConnection) url.openConnection();

BufferedInputStream reader=new BufferedInputStream(conn.getInputStream());

File dir = new File(filedir);

FileOutputStream out=new FileOutputStream(dir);

while((size=reader.read(buffers))>0){

out.write(buffers, 0, size);

}

out.flush();

reader.close();

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

//读出String文件

public static String readTxt(String filedir){

String read;

String readStr = "";

FileReader fileread;

BufferedReader bufread;

try{

fileread = new FileReader(filedir);

bufread = new BufferedReader(fileread);

try{

while((read = bufread.readLine()) != null){

readStr = readStr + read;

}

}catch(IOException e){

e.printStackTrace();

}

}catch(IOException e){

e.printStackTrace();

}

return readStr;

}

substring截取汉字后println就乱码

//保存为txt文件,all是组合好的string

public void saveMovie(String filename) {

try{

FileOutputStream txt_out=new FileOutputStream(filename);

DataOutputStream out = new DataOutputStream(txt_out);

out.writeUTF(all);

out.close();

}catch(FileNotFoundException fe) {

System.err.println(fe);

}catch(IOException ioe){

System.out.println(ioe);

}

}

这个文档第一个字符乱码,怎么回事?

展开

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐