ByteArrayOutputStream 练习
import java.io.*;
import java.net.*;
public class Main {
public static void main(String[] args) throws Exception {
// https://www.youtube.com/watch?v=fRh_vgS2dFE&feature=youtu.be
// https://images-na.ssl-images-amazon.com/images/I/81M-I12yh7L._SL1500_.jpg
// http://gdl.square-enix.com/ffxiv/inst/ffxiv-heavensward-bench.zip
// http://download.oracle.com/technetwork/java/javase/6/docs/zh/api.zip
// http://cdefgab.web.fc2.com/song.json
httpd h = new httpd("http://cdefgab.web.fc2.com/song.json");
h.std();
}
}
class httpd{
private String durl;
public httpd(String durl){
this.durl = durl;
}
public void std(){
System.out.println("sdt()-start");
try{
FileOutputStream fis = new FileOutputStream("demo.db");
ByteArrayOutputStream bao = new ByteArrayOutputStream();
InputStream is = dis();
byte[] buff = new byte[10240];
int len = 0;
System.out.println("koko");
while( (len = is.read(buff)) != -1){
fis.write(buff,0,len);
fis.flush();
bao.write(buff,0,len);
System.out.println(len);
}
fis.close();
System.out.println(bao.toString());
}catch(Exception e){
}finally{
File f = new File("demo.db");
System.out.println(f.getName());
System.out.println(f.getAbsolutePath());
}
}
public InputStream dis(){
InputStream inputStream = null;
try{
URL url = new URL(durl);
System.out.println("url = " + durl);
HttpURLConnection httpurl = (HttpURLConnection)url.openConnection();
httpurl.setConnectTimeout(3000);
httpurl.setDoInput(true);
httpurl.setReadTimeout(3000);
httpurl.setRequestMethod("GET");
if(httpurl.getResponseCode() == 200){
System.out.println("paus" + httpurl.getResponseCode());
inputStream = httpurl.getInputStream();
}
}catch(Exception e){
}
return inputStream;
}
}
2016年11月2日水曜日
2016年11月1日火曜日
HttpFileDownload 练习
HttpFileDownload 练习
https://paiza.io/projects/6lTQWbqAzvT-IEAHaRl4AA
import java.io.*;
import java.net.*;
public class Main {
public static void main(String[] args) throws Exception {
// https://www.youtube.com/watch?v=fRh_vgS2dFE&feature=youtu.be
// https://images-na.ssl-images-amazon.com/images/I/81M-I12yh7L._SL1500_.jpg
// http://gdl.square-enix.com/ffxiv/inst/ffxiv-heavensward-bench.zip
httpd h = new httpd("https://www.youtube.com/watch?v=fRh_vgS2dFE&feature=youtu.be");
h.std();
}
}
class httpd{
private String durl;
public httpd(String durl){
this.durl = durl;
}
public void std(){
System.out.println("sdt()-start");
try{
FileOutputStream fis = new FileOutputStream("demo.db");
InputStream is = dis();
byte[] buff = new byte[10240];
int len = 0;
System.out.println("koko");
while( (len = is.read(buff)) != -1){
fis.write(buff,0,len);
fis.flush();
System.out.println(len);
}
fis.close();
}catch(Exception e){
}finally{
File f = new File("demo.db");
System.out.println(f.getName());
System.out.println(f.getAbsolutePath());
}
}
public InputStream dis(){
InputStream inputStream = null;
try{
URL url = new URL(durl);
System.out.println("url = " + durl);
HttpURLConnection httpurl = (HttpURLConnection)url.openConnection();
httpurl.setConnectTimeout(3000);
httpurl.setDoInput(true);
httpurl.setReadTimeout(3000);
httpurl.setRequestMethod("GET");
if(httpurl.getResponseCode() == 200){
System.out.println("paus" + httpurl.getResponseCode());
inputStream = httpurl.getInputStream();
}
}catch(Exception e){
}
return inputStream;
}
}
https://paiza.io/projects/6lTQWbqAzvT-IEAHaRl4AA
import java.io.*;
import java.net.*;
public class Main {
public static void main(String[] args) throws Exception {
// https://www.youtube.com/watch?v=fRh_vgS2dFE&feature=youtu.be
// https://images-na.ssl-images-amazon.com/images/I/81M-I12yh7L._SL1500_.jpg
// http://gdl.square-enix.com/ffxiv/inst/ffxiv-heavensward-bench.zip
httpd h = new httpd("https://www.youtube.com/watch?v=fRh_vgS2dFE&feature=youtu.be");
h.std();
}
}
class httpd{
private String durl;
public httpd(String durl){
this.durl = durl;
}
public void std(){
System.out.println("sdt()-start");
try{
FileOutputStream fis = new FileOutputStream("demo.db");
InputStream is = dis();
byte[] buff = new byte[10240];
int len = 0;
System.out.println("koko");
while( (len = is.read(buff)) != -1){
fis.write(buff,0,len);
fis.flush();
System.out.println(len);
}
fis.close();
}catch(Exception e){
}finally{
File f = new File("demo.db");
System.out.println(f.getName());
System.out.println(f.getAbsolutePath());
}
}
public InputStream dis(){
InputStream inputStream = null;
try{
URL url = new URL(durl);
System.out.println("url = " + durl);
HttpURLConnection httpurl = (HttpURLConnection)url.openConnection();
httpurl.setConnectTimeout(3000);
httpurl.setDoInput(true);
httpurl.setReadTimeout(3000);
httpurl.setRequestMethod("GET");
if(httpurl.getResponseCode() == 200){
System.out.println("paus" + httpurl.getResponseCode());
inputStream = httpurl.getInputStream();
}
}catch(Exception e){
}
return inputStream;
}
}
HttpFileDownload 练习
练习
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Http {
public static void main(String[] args) {
// TODO Auto-generated method stub
// HttpDown hd = new
// HttpDown("http://gdl.square-enix.com/ffxiv/inst/ffxiv-heavensward-bench.zip");
HttpDown hd = new HttpDown("http://download.oracle.com/technetwork/java/javase/6/docs/zh/api.zip");
hd.SavetoDisk();
}
}
class HttpDown {
private String URL_PATH;
public HttpDown(String URL_PATH) {
this.URL_PATH = URL_PATH;
}
public void SavetoDisk() {
InputStream is = null;
FileOutputStream fo = null;
try {
fo = new FileOutputStream("e:\\testFile.zip");
is = DownStream();
byte[] buffer = new byte[1024 * 1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fo.write(buffer, 0, len);
fo.flush();
System.out.println(len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fo != null) {
fo.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public InputStream DownStream() {
InputStream inputStream = null;
HttpURLConnection hul = null;
try {
URL rul = new URL(URL_PATH);
hul = (HttpURLConnection) rul.openConnection();
hul.setConnectTimeout(3000);
hul.setReadTimeout(3000);
hul.setDoInput(true);
hul.setRequestMethod("GET");
if (hul.getResponseCode() == 200) {
inputStream = hul.getInputStream();
} else {
inputStream = null;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return inputStream;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Http {
public static void main(String[] args) {
// TODO Auto-generated method stub
// HttpDown hd = new
// HttpDown("http://gdl.square-enix.com/ffxiv/inst/ffxiv-heavensward-bench.zip");
HttpDown hd = new HttpDown("http://download.oracle.com/technetwork/java/javase/6/docs/zh/api.zip");
hd.SavetoDisk();
}
}
class HttpDown {
private String URL_PATH;
public HttpDown(String URL_PATH) {
this.URL_PATH = URL_PATH;
}
public void SavetoDisk() {
InputStream is = null;
FileOutputStream fo = null;
try {
fo = new FileOutputStream("e:\\testFile.zip");
is = DownStream();
byte[] buffer = new byte[1024 * 1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fo.write(buffer, 0, len);
fo.flush();
System.out.println(len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fo != null) {
fo.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public InputStream DownStream() {
InputStream inputStream = null;
HttpURLConnection hul = null;
try {
URL rul = new URL(URL_PATH);
hul = (HttpURLConnection) rul.openConnection();
hul.setConnectTimeout(3000);
hul.setReadTimeout(3000);
hul.setDoInput(true);
hul.setRequestMethod("GET");
if (hul.getResponseCode() == 200) {
inputStream = hul.getInputStream();
} else {
inputStream = null;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return inputStream;
}
}
java HTTP 协议下的文件下载
java HTTP 协议下的文件下载
1. 建立HTTP的链接,返回输入流。
2. 读取输入流,写入输出流。
import java.net.*;
import java.io.*;
class demo{
public static void main(String[] args) {
HttpUtils hu = new HttpUtils("http://bizingeinoutetyou.com/bizinkisya/wp-content/uploads/2015/04/kiritanimirei_i04.jpg");
hu.saveImageToDisk();
}
}
class HttpUtils{
// private static String url_path = "http://bizingeinoutetyou.com/bizinkisya/wp-content/uploads/2015/04/kiritanimirei_i04.jpg";
private static String url_path ;
public HttpUtils(String url_path){
this.url_path = url_path;
}
public void saveImageToDisk(){
InputStream inputstream = getInputStream();
FileOutputStream fileoutputstream = null;
byte[] buff = new byte[1024];
int len = 0;
try{
fileoutputstream = new FileOutputStream("test.jpg");
while((len = inputstream.read(buff)) != -1){
fileoutputstream.write(buff,0,len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(inputstream != null){
try{
inputstream.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(fileoutputstream != null){
try{
fileoutputstream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static InputStream getInputStream(){
InputStream inputstream = null;
HttpURLConnection httpurlconnection = null;
try{
URL url = new URL(url_path);
httpurlconnection = (HttpURLConnection)url.openConnection();
httpurlconnection.setConnectTimeout(3000);
httpurlconnection.setDoInput(true);
httpurlconnection.setRequestMethod("GET");
int responseCode = httpurlconnection.getResponseCode();
if(responseCode == 200){
inputstream = httpurlconnection.getInputStream();
}
System.out.println(url.getFile());
System.out.println(httpurlconnection.getResponseCode());
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return inputstream;
}
}
1. 建立HTTP的链接,返回输入流。
2. 读取输入流,写入输出流。
import java.net.*;
import java.io.*;
class demo{
public static void main(String[] args) {
HttpUtils hu = new HttpUtils("http://bizingeinoutetyou.com/bizinkisya/wp-content/uploads/2015/04/kiritanimirei_i04.jpg");
hu.saveImageToDisk();
}
}
class HttpUtils{
// private static String url_path = "http://bizingeinoutetyou.com/bizinkisya/wp-content/uploads/2015/04/kiritanimirei_i04.jpg";
private static String url_path ;
public HttpUtils(String url_path){
this.url_path = url_path;
}
public void saveImageToDisk(){
InputStream inputstream = getInputStream();
FileOutputStream fileoutputstream = null;
byte[] buff = new byte[1024];
int len = 0;
try{
fileoutputstream = new FileOutputStream("test.jpg");
while((len = inputstream.read(buff)) != -1){
fileoutputstream.write(buff,0,len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(inputstream != null){
try{
inputstream.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(fileoutputstream != null){
try{
fileoutputstream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public static InputStream getInputStream(){
InputStream inputstream = null;
HttpURLConnection httpurlconnection = null;
try{
URL url = new URL(url_path);
httpurlconnection = (HttpURLConnection)url.openConnection();
httpurlconnection.setConnectTimeout(3000);
httpurlconnection.setDoInput(true);
httpurlconnection.setRequestMethod("GET");
int responseCode = httpurlconnection.getResponseCode();
if(responseCode == 200){
inputstream = httpurlconnection.getInputStream();
}
System.out.println(url.getFile());
System.out.println(httpurlconnection.getResponseCode());
}catch(MalformedURLException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return inputstream;
}
}
2016年10月31日月曜日
Android FileWriter,FileReader
Android FileWriter,FileReader
文件的保存位置有要求
String path ="/data/data/" + this.getPackageName() +"/demo.txt";
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Test();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void Test() throws IOException {
final String tag = "testFile";
String path ="/data/data/" + this.getPackageName() +"/demo.txt";
FileWriter fileWriter = new FileWriter(path);
for( int i = 0; i <102; i++){
fileWriter.write(String.valueOf(i));
}
fileWriter.flush();
fileWriter.close();
FileReader fileReader = new FileReader(path);
char[] buf = new char[24];
int num;
while(((num = fileReader.read(buf)) != 0)){
System.out.println("---" + new String(buf,0,num));
}
fileReader.close();
}
}
文件的保存位置有要求
String path ="/data/data/" + this.getPackageName() +"/demo.txt";
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Test();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void Test() throws IOException {
final String tag = "testFile";
String path ="/data/data/" + this.getPackageName() +"/demo.txt";
FileWriter fileWriter = new FileWriter(path);
for( int i = 0; i <102; i++){
fileWriter.write(String.valueOf(i));
}
fileWriter.flush();
fileWriter.close();
FileReader fileReader = new FileReader(path);
char[] buf = new char[24];
int num;
while(((num = fileReader.read(buf)) != 0)){
System.out.println("---" + new String(buf,0,num));
}
fileReader.close();
}
}
结果
com.example.java.m1031a I/System.out: ---012345678910111213141516
com.example.java.m1031a I/System.out: ---171819202122232425262728
com.example.java.m1031a I/System.out: ---293031323334353637383940
com.example.java.m1031a I/System.out: ---414243444546474849505152
com.example.java.m1031a I/System.out: ---535455565758596061626364
com.example.java.m1031a I/System.out: ---656667686970717273747576
com.example.java.m1031a I/System.out: ---777879808182838485868788
com.example.java.m1031a I/System.out: ---899091929394959697989910
com.example.java.m1031a I/System.out: ---0101
2016年10月30日日曜日
Sublime Text 利用するプラグイン
Sublime Text 利用するプラグイン
1. PackageControl 追加
https://packagecontrol.io/installation#Simple
2. 日本語サポートする IMESupport
3. SFTP FTPサーバーと接続するため
4. F5 を押すと日付を入力する insertDate
SublimeAStyleFormatter
is a simple code formatter plugin for Sublime Text 2.
It provides ability to format C, C++, C#, and Java files.
1. PackageControl 追加
https://packagecontrol.io/installation#Simple
2. 日本語サポートする IMESupport
3. SFTP FTPサーバーと接続するため
4. F5 を押すと日付を入力する insertDate
SublimeAStyleFormatter
is a simple code formatter plugin for Sublime Text 2.
It provides ability to format C, C++, C#, and Java files.
Android應用開發視頻教程
黑马程序员 毕向东 Java基础视频教程
https://www.youtube.com/playlist?list=PLvswSo32Xlu_ctuWa-7-huGYUlsslzVhi
Android應用開發視頻教程
https://www.youtube.com/playlist?list=PLvswSo32Xlu8EVeS2RZvTg30M78WLROkT
https://www.youtube.com/playlist?list=PLvswSo32Xlu_ctuWa-7-huGYUlsslzVhi
Android應用開發視頻教程
https://www.youtube.com/playlist?list=PLvswSo32Xlu8EVeS2RZvTg30M78WLROkT
登録:
投稿 (Atom)