java中怎样将文件的内容读取成字符串

java中怎样将文件的内容读取成字符串,第1张

java中有四种将文件的内容读取成字符串

方式一:

Java code

/

 以字节为单位读取文件,常用于读二进制文件,如、声音、影像等文件。

 当然也是可以读字符串的。

/

/ 貌似是说网络环境中比较复杂,每次传过来的字符是定长的,用这种方式?/

public String readString1()

{

try

{

//FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。

FileInputStream inStream=thisopenFileInput(FILE_NAME);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte[] buffer=new byte[1024];

int length=-1;

while( (length = inStreamread(buffer) != -1)

{

boswrite(buffer,0,length);

// write方法 SDK 的解释是 Writes count bytes from the byte array buffer starting at offset index to this stream

//  当流关闭以后内容依然存在

}

bosclose();

inStreamclose();

return bostoString();

// 为什么不一次性把buffer得大小取出来呢?为什么还要写入到bos中呢? return new(buffer,"UTF-8") 不更好么

// return new String(bostoByteArray(),"UTF-8");

}

}

方式二:

Java code

方式三:

Java code

方式四:

Java code

/InputStreamReader+BufferedReader读取字符串  , InputStreamReader类是从字节流到字符流的桥梁/

/ 按行读对于要处理的格式化数据是一种读取的好方式 /

private static String readString4()

{

int len=0;

StringBuffer str=new StringBuffer("");

File file=new File(FILE_IN);

try {

FileInputStream is=new FileInputStream(file);

InputStreamReader isr= new InputStreamReader(is);

BufferedReader in= new BufferedReader(isr);

String line=null;

while( (line=inreadLine())!=null )

{

if(len != 0)  // 处理换行符的问题

{

strappend("\r\n"+line);

}

else

{

strappend(line);

}

len++;

}

inclose();

isclose();

} catch (IOException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

return strtoString();

}

public class FindType {

public static void main(String[] args) {

String s1 = "20151012mp4";

Systemoutprintln(getType(s1));//mp4

String s2 = "123007txt";//假设文件名有很多的点也可以

Systemoutprintln(getType(s2));//txt

}

//方法去寻找最后一个点开头的字符串

static String getType(String s){

String[] ss = ssplit("\\");//表示用去切割字符串

return ""+ss[sslength-1];//用连接最后一个字符串 

}

}

输出

mp4

txt

这道题主要就是利用了String类的split()方法,进行多次调用,已经帮你实现了,代码如下:

public

class

Test{

public

static

void

main(String[]

args){

String

str

=

"c:/win/good/aaatxt;d:/win/good/bbbtxt;c:/win/cccctxt;";

//得到路径数组

String[]

fileRoot

=

strsplit(";");

String[]

fileName

=

null;

for(int

i

=

0;i

<

fileRootlength;i++){

if(fileRoot[i]

!=

null){

fileName

=

fileRoot[i]split("/");

//得到最终需要的文件名

Systemoutprintln

(fileName[fileNamelength-1]);

}

}

}

}

import javaioBufferedReader;

import javaioFile;

import javaioFileInputStream;

import javaioIOException;

import javaioInputStreamReader;

public class Test1 {

public static void main(String[] args) throws IOException{

File file = new File("h:/testtxt");

FileInputStream fis = new FileInputStream(file);

InputStreamReader isr = new InputStreamReader(fis);

BufferedReader br = new BufferedReader(isr);

String str;

while((str = brreadLine()) != null){

char ch = strcharAt(8);

Systemoutprintln(ch);

}

brclose();

}

}

我累累的码完,发现已经有人写好了,看起来很专业。我是这两天刚学到流,顺便练习下,我也来学习下高人的代码!!!

首先,可以直接写入string的,这段程序的这种写法很无聊,让你误解了。\x0d\如:outwrite(p_send_text);\x0d\\x0d\其次,如果想写入一行并且换行的话,那么得包装一个printwriter,如:\x0d\PrintWriterout=newPrintWriter(FileWriter(file,true));\x0d\outprintln(p_send_text);\x0d\\x0d\在Java里,\x0d\char表示一个字符,它可以直接转换为int,byte,long(ascii/unicode码)\x0d\String表示一串字符,它可以通过某些方法转换成一个数组,如char[],byte[],也可以用其他方法取出其中某个特定位置的字符,如charAt();\x0d\\x0d\与C里面不同,在Java中,通常String用的比较多,char[]基本不用的。

BufferedReader bufferedReader = new BufferedReader(new FileReader(file));String str = null;StringBuffer sb= new StringBuffer();while((str=bufferedReaderreadLine())!=null){ sbappend(str);}bufferedReaderclose();Systemoutprintln(sb);

如果是修改文件的编码方式,可以按以下步骤:

选中文件右键-->Properties-->Resource-->Text file encoding-->选中Other,然后选择自己想要的编码格式就可以了。

如果是修改MyEclipse的编码方式,可以按以下步骤:

菜单栏的Window-->Perferences-->General-->Work Space-->Text file encoding-->选中Other,然后选择自己想要的编码格式就可以了。

以上就是关于java中怎样将文件的内容读取成字符串全部的内容,包括:java中怎样将文件的内容读取成字符串、java 字符串(文件名),得到文件格式字符串、java代码实现从路径字符串中获取文件名称等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9576533.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存