当前位置:首页 » 编程语言 » JAVA读字符

JAVA读字符

发布时间: 2023-01-11 16:44:22

1. java中读取字符和读取字符串的差别

具体的区别不是很大。先给你看看那API上的介绍。

public class FileInputStream extends InputStream
FileInputStream 从文件系统中的某个文件中获得输入字节。哪些文件可用取决于主机环境。
FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑FileReader。

public class FileReader extends InputStreamReader
用来读取字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是适当的。要自己指定这些值,可以先在 FileInputStream 上构造一个 InputStreamReader。
FileReader 用于读取字符流。要读取原始字节流,请考虑使用 FileInputStream。

public int read()
throws IOException从此输入流中读取一个数据字节。如果没有输入可用,则此方法将阻塞。

指定者:
类 InputStream 中的 read
返回:
下一个数据字节;如果已到达文件末尾,则返回 -1。
抛出:
IOException - 如果发生 I/O 错误。

public int read(byte[] b)
throws IOException从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。在某些输入可用之前,此方法将阻塞。

覆盖:
类 InputStream 中的 read
参数:
b - 存储读取数据的缓冲区。
返回:
读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
抛出:
IOException - 如果发生 I/O 错误。
另请参见:
InputStream.read(byte[], int, int)

public int read(byte[] b,
int off,
int len)
throws IOException从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。如果 len 不为 0,则在输入可用之前,该方法将阻塞;否则,不读取任何字节并返回 0。

覆盖:
类 InputStream 中的 read
参数:
b - 存储读取数据的缓冲区。
off - 目标数组 b 中的起始偏移量。
len - 读取的最大字节数。
返回:
读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
抛出:
NullPointerException - 如果 b 为 null。
IndexOutOfBoundsException - 如果 off 为负、len 为负,或者 len 大于 b.length - off
IOException - 如果发生 I/O 错误。
另请参见:
InputStream.read()

一下3个方法都是在InputStream中定义的
public abstract int read()
throws IOException从输入流中读取数据的下一个字节。返回 0 到 255 范围内的 int 字节值。如果因为已经到达流末尾而没有可用的字节,则返回值 -1。在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。
子类必须提供此方法的一个实现。

public int read(byte[] b)
throws IOException从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数。在输入数据可用、检测到文件末尾或者抛出异常前,此方法一直阻塞。
如果 b 的长度为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在 b 中。

将读取的第一个字节存储在元素 b[0] 中,下一个存储在 b[1] 中,依次类推。读取的字节数最多等于 b 的长度。设 k 为实际读取的字节数;这些字节将存储在 b[0] 到 b[k-1] 的元素中,不影响 b[k] 到 b[b.length-1] 的元素。

类 InputStream 的 read(b) 方法的效果等同于:

read(b, 0, b.length)
参数:
b - 存储读入数据的缓冲区。
返回:
读入缓冲区的总字节数;如果因为已经到达流末尾而不再有数据可用,则返回 -1。

public int read(byte[] b,
int off,
int len)
throws IOException将输入流中最多 len 个数据字节读入 byte 数组。尝试读取 len 个字节,但读取的字节也可能小于该值。以整数形式返回实际读取的字节数。
在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。

如果 len 为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在 b 中。

将读取的第一个字节存储在元素 b[off] 中,下一个存储在 b[off+1] 中,依次类推。读取的字节数最多等于 len。设 k 为实际读取的字节数;这些字节将存储在 b[off] 到 b[off+k-1] 的元素中,不影响 b[off+k] 到 b[off+len-1] 的元素。

在任何情况下,b[0] 到 b[off] 的元素以及 b[off+len] 到 b[b.length-1] 的元素都不会受到影响。

类 InputStream 的 read(b, off, len) 方法重复调用方法 read()。如果第一次这样的调用导致 IOException,则从对 read(b, off, len) 方法的调用中返回该异常。如果对 read() 的任何后续调用导致 IOException,则捕获该异常并将其视为到达文件末尾;到达该点时读取的字节存储在 b 中,并返回发生异常之前读取的字节数。在已读取输入数据 len 的请求数量、检测到文件结束标记、抛出异常前,此方法的默认实现将一直阻塞。建议子类提供此方法更为有效的实现。

参数:
b - 读入数据的缓冲区。
off - 数组 b 中将写入数据的初始偏移量。
len - 要读取的最大字节数。
返回:
读入缓冲区的总字节数;如果因为已到达流末尾而不再有数据可用,则返回 -1。

总结一下:
FileInputstream.read()读取的是单个字符,是ASCII码表所能表示的字符,要是要读取像有汉字等其他ASCII不能表示的,就要用FileReader,它读取的是java支持的字符集。
另外,像要是读取图片,pdf,psd等这些东西的话,就要用FileInputStream,即文件字节流。应为存在计算机中的就是二进制,是ASCII表示的。要是读取有汉字的.txt,.就用FileReader。其实用的较多的是BuffereReader,进行逐行读取。

2. java字符读取

InputStreamReader不属于FileInputStream的子类的对象,要在reader类内部包装stream类。 in=new InputStreamReader(new FileInputStream("e:\\java\\wen.txt"));

3. 在java中如何读入一个字符谢谢

import java.util.Scanner;
.......
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
..........
java.util.Scanner包在jdk1.5以上才支持

4. java读取字符串

public class StringDemo1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="AAAAA,mmmm,111112121,dddd,11111";

String []st=str.split(",");
for(String sc:st){
System.out.println(sc);

}

}
}

5. java怎么读取文本文件中的所有字符

可以用文件流FileInputStream的方式读取,如果文本文件太大了,不建议一次性往内存中读,那往往会使之溢出。也可以一行行的读取,用BufferReader读,具体的实例都可以网络得到的。

6. JAVA读取字符,读取一个执行一个代码怎么搞

在此提供两种方法:
1.可以用String的charAt()方法,例子如下:
for(int i=0;i<test.length();i++){
char tmp = test.charAt(i);
//即tmp为test字符串的第i个字符。
System.out.println(tmp);
}

2.也可用String的toCharArray()方法,例子如下:
String test = "test";
char[] tmp = test.toCharArray();
for (int i = 0; i < tmp.length; i++) {
//即tmp为test转成的字符数组
System.out.println(tmp[i]);
}

7. java 中怎样实现从键盘读入单个字符

可以通过”Scanner“函数 直接输入参数的形式,来实现输入语句,举例:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入一个字符:");
String length=input.next();//输入一个字符
System.out.println("输入的字符是:"+length);}
}
备注:输入字符后,通过回车的形式来确认输入即可。

8. java读取字符串操作

File fileA = new File("h:\\x.txt");
FileInputStream fis = new FileInputStream(fileA);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] data = new byte[1024 * 1024];
int i = 0;
int j = 0;
while ((i = bis.read()) != -1) {
data[j] = (byte) i;
j++;
}
bis.close();
fis.close();
File fileB = new File("h:\\y.txt");
FileOutputStream fos = new FileOutputStream(fileB);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(data, 0, j);
bos.flush();
fos.close();
bos.close();
以前写的重一个TXT文件读取 再写到另一个TXT文件 希望能帮你

9. java中读字符的函数代码!

/**
* @version 1.0
* @author 韩卫召
*
* 多种方式读文件的内容
* 按字节读取文件内容,按字符读取文件的内容,按行读取文件的内容,随即读取文件的内容
*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.util.ResourceBundle;

public class ReadFromFile {
/**
* 以字节为单位读取文件的内容,常用于二进制文件,如声音,图象,影象等文件
*
* @param filename
* 文件名
*/
public static void readFileByBytes(java.lang.String filename) {
File file = new File(filename);
InputStream in = null;
System.out.println("以字节为单位读取文件的内容,一次读一个字节: ");
// 一次读一个字节
try {
in = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int tempbyte;
try {
// 不断的读取,直到文件结束
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
System.out.println("以字节为单位读取文件内容,一次读多个字节: ");
// 一次读取多个字节
byte[] tempbytes = new byte[100];
int byteread = 0;
try {
in = new FileInputStream(filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ReadFromFile.showAvailabelBytes(in);
try {
while ((byteread = in.read(tempbytes)) != -1) {
// 读取多个字节到数组中,byteead为一次读取的字节数
System.out.write(tempbytes, 0, byteread);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void readFileByteChars(java.lang.String filename) {
/**
* 以字符为单位读取文件,常用与读文本,数字等类型的文件
*/
File file = new File(filename);
Reader reader = null;
System.out.println("以字符为单位读取文件内容,一次读一个字节: ");
// 一次读一个字符
try {
reader = new InputStreamReader(new FileInputStream(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int tempchar;
try {
while ((tempchar = reader.read()) != -1) {
// 在Window下,\r\n这两个字符在一起时,表示一个换行
// 但如果这两个字符分开显示时,会换行两次行
// 因此,屏蔽掉\r,或者\n;否则,将会多出来很多空行
if (((char) tempchar) != '\r') {
System.out.println((char) tempchar);
}
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("以字符为单位读取文件内容,一次读多个字符: ");
char[] tempchars = new char[30];
int charread = 0;
try {
reader = new InputStreamReader(new FileInputStream(filename));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// 读入多个字符到字符数组中,charread为一次读取字符数
while ((charread = reader.read(tempchars)) != -1) {
if ((charread == tempchars.length)
&& (tempchars[tempchars.length - 1] != '\r')) {
System.out.println(tempchars);
} else {
for (int i = 0; i < charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}

public static void readFileByLines(java.lang.String filename) {
File file = new File(filename);
BufferedReader reader = null;
// System.out.println("以行为单位读取文件的内容,一次读一整行: ");
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.lang.String tempString = null;
int line = 1;
try {
while ((tempString = reader.readLine()) != null) {
if(tempString.indexOf("福州")!=-1){
System.out.println("靠!出错了!赶紧报警啊! " + line + ": " + tempString);

}
// System.out.println("line " + line + ": " + tempString);
line++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

public static void readFileByRandomAccess(java.lang.String filename) {
RandomAccessFile randomFile = null;
System.out.println("随即读取一段文件内容: ");
try {
randomFile = new RandomAccessFile(filename, "r");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long fileLength = 0;
try {
fileLength = randomFile.length();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int beginIndex = (fileLength > 4) ? 4 : 0;
try {
randomFile.seek(beginIndex);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] bytes = new byte[10];
int byteread = 0;
try {
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

private static void showAvailabelBytes(InputStream in) {
try {
System.out.println("当前字节输入流中的字节数为: " + in.available());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(java.lang.String args[]) {
ResourceBundle rb = ResourceBundle.getBundle("PreventTampering");
String filePath = rb.getString("filePath");

java.lang.String filename = filePath+"test.txt";
System.out.println(filename);
// ReadFromFile.readFileByBytes(filename);
// ReadFromFile.readFileByteChars(filename);
ReadFromFile.readFileByLines(filename);
// ReadFromFile.readFileByRandomAccess(filename);
}

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

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();

}

热点内容
c语言fread返回值 发布:2025-07-12 16:57:32 浏览:680
王者荣耀在哪里显示账号密码 发布:2025-07-12 16:36:42 浏览:898
打包sql数据库 发布:2025-07-12 16:19:27 浏览:797
php日志查看 发布:2025-07-12 16:12:10 浏览:214
ftp目录映射为本地盘符 发布:2025-07-12 16:06:59 浏览:645
nas存储百科 发布:2025-07-12 16:03:17 浏览:126
python的sort函数 发布:2025-07-12 15:53:21 浏览:50
ensp服务器怎么设置web根目录 发布:2025-07-12 15:47:56 浏览:286
安卓怎么设置二卡发信息 发布:2025-07-12 15:43:50 浏览:743
如何看到无线密码 发布:2025-07-12 15:43:13 浏览:677