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就可以了。