import java.io.File;
創新互聯為企業提供:成都品牌網站建設、網絡營銷策劃、小程序設計、營銷型網站建設和網站運營托管,一站式網絡營銷整體服務。實現不斷獲取潛在客戶之核心目標,建立了企業專屬的“營銷型網站”,就用不著再為了獲取潛在客戶而苦惱,相反,客戶會主動找您,生意就找上門來了!
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Io{
public static void main(String [] s){
File filename = new File("F:\\suncity.txt");
String filein="你好!";
RandomAccessFile mm = null;
try {
mm = new RandomAccessFile(filename,"rw");
mm.writeBytes(filein);
} catch (IOException e1) {
// TODO 自動生成 catch 塊
e1.printStackTrace();
} finally{
if(mm!=null){
try {
mm.close();
} catch (IOException e2) {
// TODO 自動生成 catch 塊
e2.printStackTrace();
}
}
}
}
}
你好,代碼如下。需要修改的話,你可以根據情況修改:
class Info{ // 定義信息類
private String name = "生產者"; // 定義name屬性
private String content = "壓入子彈" ; // 定義content屬性
private boolean flag = false ; // 設置標志位
public synchronized void set(String name,String content){
if(!flag){
try{
super.wait() ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
}
this.setName(name) ; // 設置名稱
try{
Thread.sleep(300) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
this.setContent(content) ; // 設置內容
flag = false ; // 改變標志位,表示可以取走
super.notify() ;
}
public synchronized void get(){
if(flag){
try{
super.wait() ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
}
try{
Thread.sleep(300) ;
}catch(InterruptedException e){
e.printStackTrace() ;
}
System.out.println(this.getName() +
" -- " + this.getContent()) ;
flag = true ; // 改變標志位,表示可以生產
super.notify() ;
}
public void setName(String name){
this.name = name ;
}
public void setContent(String content){
this.content = content ;
}
public String getName(){
return this.name ;
}
public String getContent(){
return this.content ;
}
};
class Producer implements Runnable{ // 通過Runnable實現多線程
private Info info = null ; // 保存Info引用
public Producer(Info info){
this.info = info ;
}
public void run(){
boolean flag = false ; // 定義標記位
for(int i=0;i12;i++){
if(flag){
this.info.set("生產者","壓入子彈") ; // 設置名稱
flag = false ;
}else{
this.info.set("消費者","射出子彈") ; // 設置名稱
flag = true ;
}
}
}
};
class Consumer implements Runnable{
private Info info = null ;
public Consumer(Info info){
this.info = info ;
}
public void run(){
for(int i=0;i24;i++){
this.info.get() ;
}
}
};
public class ThreadCaseDemo03{
public static void main(String args[]){
Info info = new Info(); // 實例化Info對象
Producer pro = new Producer(info) ; // 生產者
Consumer con = new Consumer(info) ; // 消費者
new Thread(pro).start() ;
new Thread(con).start() ;
}
};
1: 使用SwetakeQRCode在Java項目中生成二維碼
下載地址
或著
這個是日本人寫的,生成的是我們常見的方形的二維碼
可以用中文
如:5677777ghjjjjj
2: 使用BarCode4j生成條形碼和二維碼
BarCode4j網址:
barcode4j是使用datamatrix的二維碼生成算法,為支持qr的算法
datamatrix是歐美的標準,qr為日本的標準,
barcode4j一般生成出來是長方形的
如:88777alec000yan
這個博客這方面說的挺清楚的:
3:zxing
zxing 這個是google的
下載地址
Java代碼:
import java.io.File;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
public class QRCodeEvents {
public static void main(String []args)throws Exception{
String text = "你好";
int width = 100;
int height = 100;
String format = "png";
Hashtable hints= new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);
File outputFile = new File("new.png");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
}
}
4:google chart api就有實現二維碼的方法
利用這個api,使用google appengine進行實現。
5:JS生成二維碼
使用jQuery-qrcode生成二維碼
先簡單說一下jquery-qrcode,這個開源的三方庫(可以從 獲?。?/p>
qrcode.js 是實現二維碼數據計算的核心類,
jquery.qrcode.js 是把它用jquery方式封裝起來的,用它來實現圖形渲染,其實就是畫圖(支持canvas和table兩種方式)
支持的功能主要有:
Js代碼:
text : "" //設置二維碼內容
Js代碼:
render : "canvas",//設置渲染方式
width : 256, //設置寬度
height : 256, //設置高度
typeNumber : -1, //計算模式
correctLevel : QRErrorCorrectLevel.H,//糾錯等級
background : "#ffffff",//背景顏色
foreground : "#000000" //前景顏色
使用方式非常簡單
Js代碼:
jQuery('#output').qrcode({width:200,height:200,correctLevel:0,text:content});
經過簡單實踐,
使用canvas方式渲染性能還是非常不錯的,但是如果用table方式,性能不太理想,特別是IE9以下的瀏覽器,所以需要自行優化一下渲染table的方式,這里就不細述了。
其實上面的js有一個小小的缺點,就是默認不支持中文。
這跟js的機制有關系,jquery-qrcode這個庫是采用 charCodeAt() 這個方式進行編碼轉換的,
而這個方法默認會獲取它的 Unicode 編碼,一般的解碼器都是采用UTF-8, ISO-8859-1等方式,
英文是沒有問題,如果是中文,一般情況下Unicode是UTF-16實現,長度2位,而UTF-8編碼是3位,這樣二維碼的編解碼就不匹配了。
解決方式當然是,在二維碼編碼前把字符串轉換成UTF-8,具體代碼如下:
function utf16to8(str) {
var out, i, len, c;
out = "";
len = str.length;
for(i = 0; i len; i++) {
c = str.charCodeAt(i);
if ((c = 0x0001) (c = 0x007F)) {
out += str.charAt(i);
} else if (c 0x07FF) {
out += String.fromCharCode(0xE0 | ((c 12) 0x0F));
out += String.fromCharCode(0x80 | ((c 6) 0x3F));
out += String.fromCharCode(0x80 | ((c 0) 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c 6) 0x1F));
out += String.fromCharCode(0x80 | ((c 0) 0x3F));
}
}
return out;
}
out.write(s.getBytes("ISO-8859-1"));
這條語句是說,將s按照ISO-8859-1編碼的方式進行編碼轉換,而ISO-8859-1編碼是單字節拉丁字符編碼(即ASCII編碼),會把雙字節的中文編碼轉換成單字節,丟失了一個字節,一般的轉換成了單字節后,該字節對應的十六進制數是3F,該數對應ASCII編碼的?所以,結果是一串問號;其實,你在把數據寫入到文件,已經發生了丟失了數據!所以最后是亂碼??!
現在解釋為什么123正常顯示,字符串在java中是以Unicode編碼方式進行編碼的,該編碼方式占2個字節,對于123這3個字符,其實也占有了6個字節,在進行編碼轉換的過程中,也丟失了一個字節,丟失的是高字節,保留了低字節,同時Unicode編碼是兼容ASCII碼的,所以可以認為沒有發生字節丟失,讀取的時候,只要是以兼容ASCII碼的編碼方式讀取,基本上不會出現亂碼!
如有不清楚地方,歡迎繼續追問!!
public class Message {
public static void main(String[] args){
String name;
int age;
System.out.println("請輸入姓名,回車結束:"); //提示輸入
Scanner sc = new Scanner(System.in);
name = sc.nextLine(); //為變量賦值
System.out.println("請輸入年齡,回車結束:");
age = sc.nextInt();
System.out.println("姓名:"+name+"\n年齡:"+age); //打印姓名及年齡
}
}
//不知道這樣行么?
當前名稱:java你好中國代碼 看懂java代碼
本文地址:http://m.kartarina.com/article24/hgscce.html
成都網站建設公司_創新互聯,為您提供企業網站制作、手機網站建設、云服務器、網站維護、小程序開發、網頁設計公司
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯