자바 io 조작
//IO: ( )
/*
*
*/
public class TestFileInputStream {
public static void main(String[] args) throws IOException {
// f
File f = new File("file/abc.txt");
//
FileInputStream fis = null;
try {
// f
fis = new FileInputStream(f);
//
// int read = fis.read();
//
//
byte[]bs = new byte[(int)f.length()];
// bs
fis.read(bs);
// ( )
String str = new String(bs);
System.out.println(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
fis.close();
}
}
}
//--------------------------------------------------------------------------
/*
*
*/
public class TestFileOutputStream {
public static void main(String[] args) {
// 。 f
File f = new File("file/def.txt");
FileOutputStream fos = null;
//
try {
// ,
//FileOutputStream(f,true)//f ,true
fos = new FileOutputStream(f,true);
//
String str = " ,
?";
//
byte[] bs = str.getBytes();
//
fos.write(bs);
//
fos.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(null!=fos){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//-------------------------------------------------------------------------
/**
*
*/
public static void main(String[] args) {
BufferedReader br = null;
try {
// ,
br = new BufferedReader(
new FileReader(
new File("file/abc.txt")));
//
String str = br.readLine();//
while(str!=null){//
System.out.println(str);
str = br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(null!=br){
try {
// ,FileReader ,
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//----------------------------------------------------------------------------------
/**
* , ( )
*/
public static void main(String[] args) {
// file/def.txt
BufferedReader br = null;
//
BufferedWriter bw = null;
try {
/* File file = new File("file/def.txt");
// FileReader ,
FileReader fr = new FileReader(file);
// , Reader
br = new BufferedReader(fr);*/
//
br = new BufferedReader(
new FileReader(
new File("file/def.txt")));
// , d aaa.txt
bw = new BufferedWriter(
new FileWriter(
new File("D:/aaa.txt")));
//
String str = br.readLine();
while(str!=null){
//
bw.write(str);
bw.newLine();//
bw.flush();//
//
str = br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
//
if(bw!=null){
bw.close();
}
if(br!=null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//-------------------------------------------------------------------------------
/**
*
* @author Administrator
:
reader
FileReader:
BufferedReader:
*/
public class TestReader {
public static void main(String[] args) {
//
FileReader fr = null;
try {
// ,
fr = new FileReader(new File("file/abc.txt"));
//
// int read = fr.read();//
//
char[]chs = new char[10];
// ,len
int len = fr.read(chs);
//
while(len!=-1){//len=-1
//
// ,len
String str = new String(chs,0,len);
System.out.println(str);
//
len = fr.read(chs);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(null!=fr){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//----------------------------------------------------------------------------------
/*
*
* properties key value String
*
* */
public class TestProper {
public static void main(String[] args) {
FileInputStream fis = null;
Properties pp = null;
try {
//
fis = new FileInputStream("file/abc.properties");
// properties
pp = new Properties();
// ,
pp.load(fis);
// pp
String name = pp.getProperty("name");
String age = pp.getProperty("age");
String hobbys = pp.getProperty("hobby");
// hobbys “ , , ”
String[] hobs = hobbys.split(",");
System.out.println(name+age+hobbys);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
( )
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.