「javarw」Javarw是什么意思

博主:adminadmin 2022-12-08 03:51:08 61

今天给各位分享javarw的知识,其中也会对Javarw是什么意思进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA文件追加的几种方式

java文件追加内容的三种方法:

方法一:

public static void writeToTxtByRandomAccessFile(File file, String str){

RandomAccessFile randomAccessFile = null;

try {

randomAccessFile = new RandomAccessFile(file,"rw");

long len = randomAccessFile.length();

randomAccessFile.seek(len);

randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n");

} catch (FileNotFoundException e) {

e.printStackTrace();

}catch (IOException e) {

e.printStackTrace();

}finally{

try {

randomAccessFile.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

方法二:

public static void writeToTxtByFileWriter(File file, String content){

BufferedWriter bw = null;

try {

FileWriter fw = new FileWriter(file, true);

bw = new BufferedWriter(fw);

bw.write(content);

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

bw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

方法三:

public static void writeToTxtByOutputStream(File file, String content){

BufferedOutputStream bufferedOutputStream = null;

try {

bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true));

bufferedOutputStream.write(content.getBytes());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch(IOException e ){

e.printStackTrace();

}finally{

try {

bufferedOutputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

javaweb,图片上传至阿里云Linux服务器,默认权限是rw-r----,外界访问不到,怎么修改默认权限?

使用umask命令可以设置创建新文件时的默认权限。

语法:umask [选项] [权限掩码]

参数说明:-S 以字符的方式来表示权限掩码

示例:设置文件的权限掩码,使新建文件自动生成默认权限为rw-rw----

说明:rw-rw----对应的数字就是660 ,在Linux系统中,读权限(read,r)的值是4,写权限(write,w)的值是2,执行权限(execute,x)的值是1,没有授权的值是0 ,所以rw-rw----的数字权限值就是660。因为是建立新的文件,Linux默认不允许用户建立的文件具备可执行权限,所以文件的最大值权限值是666,umask值实际上是计算的最大权限值的补码,所以此时umask值是666-660=006

所以,要依次执行如下命令:

linux@server:~$ umask 006

linux@server:~$ touch abc

linux@server:~$ ll abc

-rw-rw---- 1 linux linux 0 2010-12-11 21:44 abc

扩展:两个比较特殊的权限掩码

umask 777 #创建文件会没有任何权限,他和chmod是相反的。

umask 000 #具有所有权限。但文件没有x权限。

复制粘贴,原网址:

Java中哪个类提供了随机访问文件的功能

java.io.RandomAccessFile类,有两个构造方法。

RandomAccessFile(File file, String mode)

RandomAccessFile(String name, String mode)

mode 参数指定用以打开文件的访问模式。允许的值及其含意为:

"r"    

以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。   

"rw"    

打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。   

"rws"    

打开以便读取和写入,对于 "rw",还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备。   

"rwd"      

打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到底层存储设备。  

java编写程序实现以下功能

/*

(1)产生5000个1~9999之间的随机整数,并将其存入文本文件a.txt中。

(2)从文件中读取这五千个整数,并计算其最大值、最小值、平均值。

*/

import java.io.*;

import java.util.*;

public class Test {

public static void main(String[] args) {

String fileName = "a.txt";

WriteData(5000, fileName);

ReadData(fileName);

}

//产生num个1~9999之间的随机整数,并将其存入fileName文件

public static void WriteData(int num, String fileName){

try{

Writer fw = new FileWriter(fileName);

int i, temp;

Random rand = new Random();

for(i=0; inum; i++){

do{

temp = rand.nextInt(10000); //产生[0, 10000)之间的随机数

}while(temp1 || temp9999);

fw.write(temp + "\n");

}

fw.flush();

fw.close();

}

catch(Exception e){

e.printStackTrace();

}

}

//从fileName文件中读取所有整数,并计算其最大值、最小值、平均值

public static void ReadData(String fileName){

String str;

int count = 0;

int temp;

int max = 0, min = 10000;

long sum = 0;

double avg;

try{

BufferedReader br = new BufferedReader(new FileReader(fileName));

while((str = br.readLine()) != null){

count++;

temp = Integer.parseInt(str);

sum += temp;

if(temp max){

max = temp;

}

if(temp min){

min = temp;

}

}

avg = 1.0 * sum / count;

System.out.println("最大值:" + max);

System.out.println("最小值:" + min);

System.out.println("平均值:" + avg);

}

catch(Exception e){

e.printStackTrace();

}

}

}

Java中有几种类型的流?

FileInputSream类

FileInputStream(String name)

FileInputStream(File file)

第一个构造方法是用给定的文件名name创建一个FileInputStream对象

第二个方法使用File对象创建FileInputStream对象

使用文件输入流读取文件

例如

FileInputStream istream=new FileInputStream(“myfile.dat”);

File f=new File(“myfile.dat”);

FileInputStream istream =new FileInputStream(f);

处理IO异常

Try{

FileInputStream istream=new FileInputStream(“myfile.dat”);

}

Catch(IOException e){}

从输入流中读取字节

Int read();从输入流中顺序读取源中的单个字节数据,返回字节值(0~255之间的一个整数),如果到达源的末尾,则返回-1

Read方法还可以吧多个字节读入到字节数组中:

Int read(byte b[]);

Int read(byte b[],int off,int len);

关闭流

使用close();

FileOutputStream类

该类提供了基本的文件写入能力,除了从OutputStream类继承来的方法外,FileOutputStream类还提供了两个构造方法:

FileOutputStream(String name)

FileOutputStream(File file)

第一个构造方法,给指定的文件名name创建一个FileOutputStreak对象

第二个构造方法,使用File对象创建FileOutputStream对象

可以使用write()方法把字节写入到输出流到达目的地

Write的格式:

Public void write(byte b[]) 其功能就是把b.length个字节到输出流

Public void write(byte b[],int off,int len)给定字节数组中,偏移off处写入len个字节到输出流

FileOutputStream流顺序的写文件,只要不关闭流,每次调用write方法就顺序地向输出流写入内容,直到流被关闭。

以上代码见hyh.io--File/Fileinputstream/Fileoutputsream

以上输入输出流容易出现的问题就是:汉字乱码

FileReader类和FileWriter类

与FileInputStream和FileOutputStream字节流相对应的是FileReader和FileWriter字符流,其分别是Reader和Writer的子类,构造方法如下:

FileReader(String filename)

FileWriter(String filename)

使用字节读取文件时,字节流不能直接操作Unicode字符,所以Java提供字符流。由于汉字在文件中占用了两个字节,如果使用字节流,读取不当会出现乱码现象,采用字符流就可以避免这个现象,因为在Unicode字符中,一个汉字被看做一个字符。

如果需要每次读取一行数据,FileReader类没有提供这样的方法,所以必须把这个流再接到另外一个流伤,从后面的流中读取一行数据。Java为其命名为BufferedReader类,构造方法:

BufferedReader(Reader in)

BufferedReader流能够读取文本行,方法是readLine()

通过向BufferedReader传递一个Reader对象,来创建一个BufferedReader对象,

FileReader inone=new FileReader(“Student.txt”);

BufferedReader intwo=BufferedReader(inone);

类似的可以将BufferedWriter流和FileWriter流连接在一起,然后使用BufferedWriter流将数据写到目的地,

FileWriter tofile=new FileWriter(”hello.txt”);

BufferedWriter out=BufferedWriter(tofile)

然后out使用BufferedReader类的方法:

Write(String s,int off,int len)把字符串s写入到文件中,off是s开始处的偏移量,len是写入的字符数量

代码详情见hyh.javaio.FileReaderandWriter

代码总结:FileReader和FileWriter可以进行汉字的读取和写入

BufferedReader有读取行的功能,readLine()

RandomAccessFile类

RandomAccessFile类创建的流指向既可以作为源也可以作为目的地。换句话说,当想对一个文件进行读写操作时,可以创建一个指向该文件的RandomAccessFile流

构造方法:

RandomAccessFile(String name,String mode)参数name用来确定一个文件名,给出流的源。参数mode取r或者rw(只读或者读写),决定对文件的访问权限。

RandomAccessFile(File file,String mode)参数file是一个File对象,给出源,同时也是目的地。参数mode决定访问权限。

该类中有一个seek(long a)方法,用来定位RandomAccessFile流的读写位置。参数a确定读写位置距离文件开头的字节个数。

还可以调用getFilePointer()方法或者流当前的读写位置

类方法P280:

关闭文件

获取读写位置等。。。。。。。。

使用RandomAccessFile流实现一个通讯录的录入与显示系统

见RandomAccessFile

数据流P284

数组流P286

对象流P288

序列化与对象克隆P290

文件锁FileLockP293

Process类中的流P295

带进度条的输入流P298(这个是我自己整理的一部分的学习笔记,教材为Java2实用教程第三版(耿祥义编著),因为使用了word进行整理,首字母大写忘记取消和一些代码在myeclipse上,但是不妨碍你查看)

javarw的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Javarw是什么意思、javarw的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-08,除非注明,否则均为首码项目网原创文章,转载请注明出处。