「javaseek函数」JAVA函数
本篇文章给大家谈谈javaseek函数,以及JAVA函数对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、初学者java编程 提示用户输入一个一维数组,为这个数组添加元素
- 2、java中有将数据存入文件并保存的函数吗
- 3、Java中的小问题:读出file1.txt的内容,输出到标准设备,验证写入的内容。
初学者java编程 提示用户输入一个一维数组,为这个数组添加元素
public static void main(String[] args) throws IOException {
System.out.println("添加元素方法开始");
String[] arr=add();
System.out.println("添加后数组元素是:");
printArr(arr);
System.out.println("获取指定位置元素方法开始");
System.out.println(getIndex(arr));
System.out.println("删除元素方法开始");
arr=delete(arr);
System.out.println("删除后数组元素是:");
printArr(arr);
}
public static String[] add() {
Scanner sc=new Scanner(System.in);
System.out.println("请输入数组元素个数,回车确认");
int n=Integer.parseInt(sc.nextLine());
System.out.println("请输入数组元素,回车确认");
String[] arr=new String[n];
for(int i=0; i n; i++) {// 循环,从控制台读数据放到数组里
arr[i]=sc.nextLine();
}
return arr;
}
public static String[] delete(String[] arr) {
Scanner sc=new Scanner(System.in);
System.out.println("要删除第几个元素?(数组脚标从0开始),回车确认");
int n=Integer.parseInt(sc.nextLine());
String[] tmp=new String[arr.length - 1];
if(n = arr.length) {
System.out.println("脚标超出数组长度,删除失败");
return arr;
}
for(int i=n; i arr.length - 1; i++) {
arr[i]=arr[i + 1];// 从位置n开始,后面的元素覆盖前面的元素
}
for(int i=0; i arr.length - 1; i++) {
tmp[i]=arr[i];// 将数组赋值给新数组,不要最后一个元素,因为数组长度-1了
}
return tmp;
}
public static String getIndex(String[] arr) {
Scanner sc=new Scanner(System.in);
System.out.println("要获取第几个元素?(数组脚标从0开始),回车确认");
int index=Integer.parseInt(sc.nextLine());
if(index = arr.length) {
System.out.println("指定位置超出数组大小");
return null;
} else {
System.out.println("获取到的是:");
return arr[index];
}
}
public static void printArr(String[] arr) {
for(int i=0; i arr.length; i++) {
System.out.print(arr[i] + ",");
}
System.out.println();
}
java中有将数据存入文件并保存的函数吗
java.io包和java.nio等都可以做到啊
比较常用的是IO包
IO输出比如图片,视频,音频等使用字节流
IO输出文字,字符串往往使用字符流(也可以使用字节流,但是比使用字节流方便)
但是字符流很多时候得考虑输入流的字符,采用指定字符集的办法,防止乱码
inputStream:所有字节输入流的父类.
outputStream:所有字节输出流的父类.
Reader:所有字符输入流的父类.
Writer:所有字符输出流的父类.
最后,你得注意流用完了得关闭流,
应用举例
import java.io.*;
public class Demo {
public static void main(String[] args) {
OutputStreamWriter bw = null;
try {//指定输出的位置和字符集
bw = new OutputStreamWriter(new FileOutputStream("c:\\abc.txt"), "utf-8");
bw.write("你好\n");
bw.write("再见");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();//流用完要关闭
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Java中的小问题:读出file1.txt的内容,输出到标准设备,验证写入的内容。
标准设备 通常指你的显示器,可以是控制台,也可以是文件
你可以读入file1.txt文件后通过system.out.println();输出是最简单的
例子:
java中多种方式读文件
一、多种方式读文件内容。
1、按字节读取文件内容
2、按字符读取文件内容
3、按行读取文件内容
4、随机读取文件内容
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
public class ReadFromFile {
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
* @param fileName 文件的名
*/
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) {
}
}
}
}
/**
* 以字符为单位读取文件,常用于读文本,数字等类型的文件
* @param fileName 文件名
*/
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下,rn这两个字符在一起时,表示一个换行。
//但如果这两个字符分开显示时,会换两次行。
//因此,屏蔽掉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
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) {
}
}
}
}
/**
* 以行为单位读取文件,常用于读面向行的格式化文件
* @param fileName 文件名
*/
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) {
}
}
}
}
/**
* 随机读取文件内容
* @param fileName 文件名
*/
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) {
}
}
}
}
/**
* 显示输入流中还剩的字节数
* @param in
*/
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);
}
}
关于javaseek函数和JAVA函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-27,除非注明,否则均为
原创文章,转载请注明出处。