导航:首页 > 编程语言 > java读string

java读string

发布时间:2022-10-09 03:47:59

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

阅读全文

与java读string相关的资料

热点内容
怎么在电脑上编译成功 浏览:214
单片机可调时钟设计方案 浏览:192
qq文件夹密码忘记怎么找回 浏览:683
php扩展插件 浏览:607
解压视频厕所抽纸 浏览:952
app减脂怎么用 浏览:452
pythonwebpdf 浏览:639
单片机的功能模块 浏览:771
安卓手机如何录制视频长时间 浏览:285
安全问题app哪个好 浏览:445
压缩水会变冰吗 浏览:526
小说配音app哪个靠谱 浏览:820
编译iso 浏览:944
照片生成pdf格式 浏览:194
病历转pdf 浏览:835
云服务器配硬件 浏览:978
服务器10k什么意思 浏览:21
pdfeditor汉化 浏览:884
新科学pdf 浏览:748
现在还有c语言编译吗 浏览:676