『壹』 java :从一个文本文件中读取所有字符,并输出。求大神指教
public static void main(String[] args) {
try {
FileReader fileReader = new FileReader("C:/Users/hsh/Desktop/a.wsdl");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String result = null;
while(bufferedReader.ready()){
result += bufferedReader.readLine();
}
System.out.println(result);
fileReader.close();
} catch (FileNotFoundException e) {
System.out.println("文件路径不对,请核对!");
} catch (IOException e) {
System.out.println("输入过程中意外地到达文件尾或流尾");
}
}
『贰』 java中怎样将文件的内容读取成字符串
方式一:
Java code
/**
*以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*当然也是可以读字符串的。
*/
/*貌似是说网络环境中比较复杂,每次传过来的字符是定长的,用这种方式?*/
publicStringreadString1()
{
try
{
//FileInputStream用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用FileReader。
FileInputStreaminStream=this.openFileInput(FILE_NAME);
ByteArrayOutputStreambos=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlength=-1;
while((length=inStream.read(buffer)!=-1)
{
bos.write(buffer,0,length);
//.write方法SDK的解释是m.
//当流关闭以后内容依然存在
}
bos.close();
inStream.close();
returnbos.toString();
//为什么不一次性把buffer得大小取出来呢?为什么还要写入到bos中呢?returnnew(buffer,"UTF-8")不更好么?
//returnnewString(bos.toByteArray(),"UTF-8");
}
}
方式二:
Java code

方式四:
Java code
/*InputStreamReader+BufferedReader读取字符串,InputStreamReader类是从字节流到字符流的桥梁*/
/*按行读对于要处理的格式化数据是一种读取的好方式*/
()
{
intlen=0;
StringBufferstr=newStringBuffer("");
Filefile=newFile(FILE_IN);
try{
FileInputStreamis=newFileInputStream(file);
InputStreamReaderisr=newInputStreamReader(is);
BufferedReaderin=newBufferedReader(isr);
Stringline=null;
while((line=in.readLine())!=null)
{
if(len!=0)//处理换行符的问题
{
str.append(" "+line);
}
else
{
str.append(line);
}
len++;
}
in.close();
is.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnstr.toString();
}
『叁』 java 文件读写中哪个方法能够统计读取的文件中的字符个数
public static void tongji(String file)throws IOException{
Map< Character, Integer> map=new HashMap<Character, Integer>();
InputStreamReader in=new InputStreamReader(new FileInputStream(file));
int b;
int all=0;
while((b=in.read())!=-1){
char ch=(char)b;
if(b=='\n'||b=='\r'||b==','||b=='。'||b==' '){
continue;
}
Integer n=map.get(ch);
map.put(ch,n==null?1:n+1);
all++;
}
ArrayList<Entry<Character,Integer>> list=new ArrayList<Map.Entry<Character,Integer>>(map.entrySet());
Collections.sort(list,new Comparator<Entry<Character,Integer>>() {
@Override
public int compare(Entry<Character, Integer> o1,
Entry<Character, Integer> o2) {
return o2.getValue()-o1.getValue();
}
});
System.out.println("总字数"+all);
System.out.println("出现的字符数:"+list.size());
for(Entry entry:list){
char ch=(Character) entry.getKey();
int c=(Integer) entry.getValue();
System.out.println(ch+"="+c);
}
}
『肆』 java读取TXT文件,然后截取字符串
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.util.Arrays;
importjava.util.Scanner;
publicclass${
publicstaticvoidmain(String[]args){
try{
Scannerin=newScanner(newFile("D:/a.sql"));
while(in.hasNextLine()){
Stringstr=in.nextLine();
String[]arr=str.split(",");
Float[]result=newFloat[arr.length-1];
for(inti=0;i<result.length;i++){
result[i]=Float.parseFloat(trim(arr[i+1]));
}
System.out.println(Arrays.toString(result));
}
in.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
}
privatestaticStringtrim(Stringstr){
str=str.replaceAll("'","");
str=str.replaceAll(";","");
returnstr;
}
}
『伍』 java中如何像readLine()读取文件一样读取字符串
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadFileTest {
public static void main(String[] args) throws IOException {
String temp="";
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(new File("d:/demo.txt")));
byte b[]=new byte[1024*1024];
int length=bis.read(b);
temp=new String(b,0,length);
bis.close();
String str[]=temp.split("\n");
for(int i=0;i<str.length;i++){
System.out.print(str[i]);
}
}
}
已经给楼主做出来了...
不知道是不是楼主想要的,
如果不是楼主想想的话...给我讲一下...
我再给楼主修改.....
祝楼主早日成功!
『陆』 JAVA中读取文件(二进制,字符)内容的几种方
JAVA中读取文件内容的方法有很多,比如按字节读取文件内容,按字符读取文件内容,按行读取文件内容,随机读取文件内容等方法,本文就以上方法的具体实现给出代码,需要的可以直接复制使用
public class ReadFromFile {
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
// 一次读一个字节
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
// 一次读多个字节
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 读入多个字节到字节数组中,byteread为一次读入的字节数
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
*/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
// 一次读一个字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
// 一次读多个字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars)) != -1) {
// 同样屏蔽掉\r不显示
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 随机读取文件内容
*/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("随机读取一段文件内容:");
// 打开一个随机访问文件流,按只读方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件长度,字节数
long fileLength = randomFile.length();
// 读文件的起始位置
int beginIndex = (fileLength > 4) ? 4 : 0;
// 将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
// 将一次读取的字节数赋给byteread
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}
/**
* 显示输入流中还剩的字节数
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("当前字节输入流中的字节数为:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}
『柒』 java 逐行读取字符串,是字符串,不是文件。 如: 我们 你 她 他们
/n 。先用这个切分下成数组可以么?
不知道你为什么要这么做。
『捌』 怎样用Java读取txt文件中每行的第9个字符请给出代码,谢谢~~
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test1 {
public static void main(String[] args) throws IOException{
File file = new File("h:/test.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String str;
while((str = br.readLine()) != null){
char ch = str.charAt(8);
System.out.println(ch);
}
br.close();
}
}
我累累的码完,发现已经有人写好了,看起来很专业。我是这两天刚学到流,顺便练习下,我也来学习下高人的代码!!!
『玖』 java从读取的文本中 取出指定字符串
这个不难。读复取文本就当作读取流的制形式。
具体实现
//读取一个文本的字符流
BufferedReader in = new BufferedReader(new FileReader("F:\\json.txt"));
String line = null;
//定义一个空字符串来接受读到的字符串
String str="";
//循环把读取到的字符赋给str
while((line = in.readLine())!=null)
{
str+=line;
}
System.out.println("str="+str);
//判断str中是否有EFG子串,为true则说明有。进去if语句
if(str.contains("EFG")){
System.out.println("yes!");
//取得子串的初始位置
int i=str.indexOf("EFG");
//根据的要取的内容后多少字符+多少个
String strEFG=str.substring(i,i+3);
System.out.println("strEFG="+strEFG);
}
『拾』 如何用Java读取一个txt文件,并将文件内容保存到String类型的变量中求Java代码!
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) throws IOException {
String fileContent = readFileContent("");
System.out.println(fileContent);
}
//参数string为你的文件名
private static String readFileContent(String fileName) throws IOException {
File file = new File(fileName);
BufferedReader bf = new BufferedReader(new FileReader(file));
String content = "";
StringBuilder sb = new StringBuilder();
while(content != null){
content = bf.readLine();
if(content == null){
break;
}
sb.append(content.trim());
}
bf.close();
return sb.toString();
}
}