A. java讀取text文件為String,如何實現
/**
* 主要是輸入流的使用,最常用的寫法
* @param filePath
* @return
*/
public static String read(String filePath)
{
// 讀取txt內容為字元串
StringBuffer txtContent = new StringBuffer();
// 每次讀取的byte數
byte[] b = new byte[8 * 1024];
InputStream in = null;
try
{
// 文件輸入流
in = new FileInputStream(filePath);
while (in.read(b) != -1)
{
// 字元串拼接
txtContent.append(new String(b));
}
// 關閉流
in.close();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return txtContent.toString();
}
B. java讀取txt中的string和int
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class wenjian
{
public static void main(String[]str)
{
wenjian2 wj2=new wenjian2();
}
}
class wenjian2 extends Frame
{
public wenjian2()
{
setVisible(true);
//setSize(200,200);
TextArea tarea=new TextArea(8,26);
add(tarea);
byte byt[]=new byte[8];
pack();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{System.exit(0);}});
try{
FileInputStream fis=new FileInputStream(new File("D:\\a.txt"));
while((fis.read(byt,0,1)!=-1))
{
tarea.append(new String(byt,0,2));
}
fis.close();
}
catch(Exception ex)
{System.out.print(ex.getMessage().toString());}
}
}
C. java如何讀取字元串中的某一段字元串
可以用substring方法來實現。
參考代碼:
Stringstr="helloword!";
System.out.println(str.substring(1,4));
System.out.println(str.substring(3,5));
System.out.println(str.substring(0,4));
將得到結果為:
ell
lo
hell
如果startIndex和endIndex其中有越界的將會拋出越界異常。
String.substring(intbeginIndex,intendIndex)
參數:
beginIndex 開始位置索引
endIndex 結束位置索引
返回:
從beginIndex位置到endIndex位置內的字元串
D. java IO流能否在程序里讀取String
可以!代碼如下: import java.io.*;
public class NumberInput1
{
public static void main(String args[])
{
try{
InputStreamReader ir;
BufferedReader in;
System.out.println("Please input: ");
ir=new InputStreamReader(System.in);
//從鍵盤接收了一個字元串的輸入,並創建了一個字元輸入流的對象
in=new BufferedReader(ir);
String s[10]
for i=1 to 10
s[i]= in.readLine();
//從輸入流in中讀入一行,並將讀取的值賦值給字元串變數s
for i=1 to 10
System.out.println("The Input Value is: "+s[i]);
in.close(); ir.close();
}catch(IOException e){
System.out.println(e);
}
}
}
E. java 分行讀取string類型數據 並存放在mysql資料庫中!
大概思路:
while(readLine){
split(" ")
insert
}
F. java讀取string字元串只讀取非數字的字元串
publicclassTest{
publicstaticvoidmain(String[]args){
Stringstr="張三13312341234";
Stringresult=str.replaceAll("[0-9]","");//正則表達式
System.out.println(result);
Stringresult2=newString();
for(charch:str.toCharArray()){
if(ch>='0'&&ch<='9'){
}
else{
result2+=ch;
}
}
System.out.println(result2.toString());
}
}
G. 如何實現java讀取text文件為String
BufferedReader br=new BufferedReader(new FileReader(fileName));
String line="";
StringBuffer buffer = new StringBuffer();
while((line=br.readLine())!=null){
buffer.append(line);
}
String fileContent = buffer.toString();
H. Java中的string什麼意思
string,java中的字元串。 String類是不可變的,對String類的任何改變,都是返回一個新的String類對象。 String 對象是 System.Char 對象的有序集合,用於表示字元串。String 對象的值是該有序集合的內容,並且該值是不可變的.
在編程語言中字元串都是用string來給定。
I. java string中讀出數字
可以選擇subtring(firstindex,lastIndex)的方法截取字元串,或者你可以試著把所有的字元串拼接起來,中間用間隔符「,」或「*」等隔開,讀取的時候可以用splite()把字元串分割成數組,然後遍歷數組。
J. JAVA如何從命令行讀入一個String
比如你現在有一個可以運行的class文件叫做A.class。那麼在命令行中可以這樣執行這個程序:
java A arg
其中arg是A需要的參數。那麼A是怎麼得到這個arg的呢。基本上是這樣的:
public static void main(String[] args)
{
if(args.length > 0)
{
String str = arg[0];
}
}
這是在A.java中的main函數,這里只是得到了第一個參數的做法,如果想得到更多的參數,只要做一個簡單的循環遍例這個數組args就可以了。