把int轉換為char把int轉換為char(例)
阿新 • • 發佈:2019-02-05
int a = 12345678901;
char str[4];
str[0] = (char)(a & 0xff);
str[1] = (char)((a > > 8) & 0xff);
str[2] = (char)((a > > 16) & 0xff);
if (sInputString != null && !sInputString.trim().equals("")){
try{
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
return tInputStringStream;
}catch (Exception ex){
ex.printStackTrace();
}
}
return null;
}
//////////////////////////////////////////////////////////////////////////
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class ReadByte {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader(
"c:/bsmain_runtime.log"));
String s;
while (br.ready() && (s = br.readLine()) != "bye") {
System.out.println("Read: " + s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上是直接從檔案
===================
一下是從String
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.PrintWriter;
class ReadByte {
public static void main(String[] args) {
try {
String test = "1234\r\nsdfs";
PipedWriter pw = new PipedWriter();
PipedReader pr = new PipedReader(pw);
PrintWriter pw2 = new PrintWriter(new BufferedWriter(pw), true);
BufferedReader br = new BufferedReader(pr);
pw2.println(test);
String s;
while (br.ready() && (s = br.readLine()) != "bye") {
System.out.println("Read: " + s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
char str[4];
str[0] = (char)(a & 0xff);
str[1] = (char)((a > > 8) & 0xff);
str[2] = (char)((a > > 16) & 0xff);
str[3] = (char)((a > > 24) & 0xff);
將字元型轉換字串
通過String.valueOf(char)函式把字元轉化成字串
舉例
char a='A';//定義一個字元a String str = String.valueOf(a);//把字元a轉換成字串str
最簡單的方法 char c = 'a'; String s = "" + c;
char[ ]轉String
String str = String.valueOf(char[] data);
把字串轉化為輸入流
public static InputStream getStringStream(String sInputString){if (sInputString != null && !sInputString.trim().equals("")){
try{
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
return tInputStringStream;
}catch (Exception ex){
ex.printStackTrace();
}
}
return null;
}
//////////////////////////////////////////////////////////////////////////////
//txt為TextArea內的字串 try{ InputStream myIn=new ByteArrayInputStream(txt.getBytes("UTF-8")); //將System.in轉化為面向字元的流 InputStreamReader ir = new InputStreamReader(myIn); in = new BufferedReader(ir);//為輸入流提供緩衝區 while ((s = in.readLine()) != "bye") System.out.println("Read: " + s); } catch(IOException e) {System.out.println("Error! ");} }
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class ReadByte {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader(
"c:/bsmain_runtime.log"));
String s;
while (br.ready() && (s = br.readLine()) != "bye") {
System.out.println("Read: " + s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上是直接從檔案
===================
一下是從String
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.PrintWriter;
class ReadByte {
public static void main(String[] args) {
try {
String test = "1234\r\nsdfs";
PipedWriter pw = new PipedWriter();
PipedReader pr = new PipedReader(pw);
PrintWriter pw2 = new PrintWriter(new BufferedWriter(pw), true);
BufferedReader br = new BufferedReader(pr);
pw2.println(test);
String s;
while (br.ready() && (s = br.readLine()) != "bye") {
System.out.println("Read: " + s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
////////////////////////////////////////////////////////////////////////////////