1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | //將C盤一個文本文件複製到D盤。 /* 複製的原理: 其實就是將C盤下的文件數據存儲到D盤的一個文件中。 步驟: 1,在D盤創建一個文件。用於存儲C盤文件中的數據。 2,定義讀取流和C盤文件關聯。 3,通過不斷的讀寫完成數據存儲。 4,關閉資源。 */ import java.io.*; class CopyText {
public static void main( String [] args) throws IOException
{
copy_2();
}
public static void copy_2()
{
FileWriter fw = null ;
FileReader fr = null ;
try
{
fw = new FileWriter( "SystemDemo_copy.txt" );
fr = new FileReader( "SystemDemo.java" );
char[] buf = new char[ 1024 ];
int len = 0 ;
while ((len=fr.read(buf))!=- 1 )
{
fw.write(buf, 0 ,len);
}
}
catch (IOException e)
{
throw new RuntimeException( "讀寫失敗" );
}
finally
{
if (fr!= null )
try
{
fr.close();
}
catch (IOException e)
{
}
if (fw!= null )
try
{
fw.close();
}
catch (IOException e)
{
}
}
}
//從C盤讀一個字符,就往D盤寫一個字符。
public static void copy_1()throws IOException
{
//創建目的地。
FileWriter fw = new FileWriter( "RuntimeDemo_copy.txt" );
//與已有文件關聯。
FileReader fr = new FileReader( "RuntimeDemo.java" );
int ch = 0 ;
while ((ch=fr.read())!=- 1 )
{
fw.write(ch);
}
fw.close();
fr.close();
} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | /* 複製一個圖片 思路: 1,用字節讀取流對象和圖片關聯。 2,用字節寫入流對象創建一個圖片文件。用於存儲獲取到的圖片數據。 3,通過循環讀寫,完成數據的存儲。 4,關閉資源。 */ import java.io.*; class CopyPic {
public static void main( String [] args)
{
FileOutputStream fos = null ;
FileInputStream fis = null ;
try
{
fos = new FileOutputStream( "c:\\2.bmp" );
fis = new FileInputStream( "c:\\1.bmp" );
byte[] buf = new byte[ 1024 ];
int len = 0 ;
while ((len=fis.read(buf))!=- 1 )
{
fos.write(buf, 0 ,len);
}
}
catch (IOException e)
{
throw new RuntimeException( "複製文件失敗" );
}
finally
{
try
{
if (fis!= null )
fis.close();
}
catch (IOException e)
{
throw new RuntimeException( "讀取關閉失敗" );
}
try
{
if (fos!= null )
fos.close();
}
catch (IOException e)
{
throw new RuntimeException( "寫入關閉失敗" );
}
}
} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.io.*; class TransStreamDemo {
public static void main( String [] args) throws IOException
{
//獲取鍵盤錄入對象。
//InputStream in = System.in;
//將字節流對象轉成字符流對象,使用轉換流。InputStreamReader
//InputStreamReader isr = new InputStreamReader(in);
//爲了提高效率,將字符串進行緩衝區技術高效操作。使用BufferedReader
//BufferedReader bufr = new BufferedReader(isr);
//鍵盤的最常見寫法。
BufferedReader bufr =
new BufferedReader( new InputStreamReader(System. in ));
// OutputStream out = System.out; // OutputStreamWriter osw = new OutputStreamWriter(out); // BufferedWriter bufw = new BufferedWriter(osw);
BufferedWriter bufw = new BufferedWriter( new OutputStreamWriter(System.out));
String line = null ;
while ((line=bufr.readLine())!= null )
{
if ( "over" .equals(line))
break ;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
bufr.close();
} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import java.io.*; import java.util.*; class PropertiesDemo {
public static void main( String [] args) throws IOException
{
//method_1();
loadDemo();
}
public static void loadDemo()throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream( "info.txt" );
//將流中的數據加載進集合。
prop.load(fis);
prop.setProperty( "wangwu" , "39" );
FileOutputStream fos = new FileOutputStream( "info.txt" );
prop.store(fos, "haha" );
// System.out.println(prop);
prop.list(System.out);
fos.close();
fis.close();
}
//演示,如何將流中的數據存儲到集合中。
//想要將info.txt中鍵值數據存到集合中進行操作。
/*
1,用一個流和info.txt文件關聯。
2,讀取一行數據,將該行數據用"="進行切割。
3,等號左邊作爲鍵,右邊作爲值。存入到Properties集合中即可。
*/
public static void method_1()throws IOException
{
BufferedReader bufr = new BufferedReader( new FileReader( "info.txt" ));
String line = null ;
Properties prop = new Properties();
while ((line=bufr.readLine())!= null )
{
String [] arr = line.split( "=" );
///System.out.println(arr[0]+"...."+arr[1]);
prop.setProperty(arr[ 0 ],arr[ 1 ]);
}
bufr.close();
System.out.println(prop);
} // 設置和獲取元素。
public static void setAndGet()
{
Properties prop = new Properties();
prop.setProperty( "zhangsan" , "30" );
prop.setProperty( "lisi" , "39" ); // System.out.println(prop);
String value = prop.getProperty( "lisi" );
//System.out.println(value);
prop.setProperty( "lisi" , 89 + "" );
Set< String > names = prop.stringPropertyNames();
for ( String s : names)
{
System.out.println(s+ ":" +prop.getProperty(s));
}
}
} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | //將C盤一個文本文件複製到D盤。 /* 複製的原理: 其實就是將C盤下的文件數據存儲到D盤的一個文件中。 步驟: 1,在D盤創建一個文件。用於存儲C盤文件中的數據。 2,定義讀取流和C盤文件關聯。 3,通過不斷的讀寫完成數據存儲。 4,關閉資源。 */ import java.io.*; class CopyText {
public static void main( String [] args) throws IOException
{
copy_2();
}
public static void copy_2()
{
FileWriter fw = null ;
FileReader fr = null ;
try
{
fw = new FileWriter( "SystemDemo_copy.txt" );
fr = new FileReader( "SystemDemo.java" );
char[] buf = new char[ 1024 ];
int len = 0 ;
while ((len=fr.read(buf))!=- 1 )
{
fw.write(buf, 0 ,len);
}
}
catch (IOException e)
{
throw new RuntimeException( "讀寫失敗" );
}
finally
{
if (fr!= null )
try
{
fr.close();
}
catch (IOException e)
{
}
if (fw!= null )
try
{
fw.close();
}
catch (IOException e)
{
}
}
}
//從C盤讀一個字符,就往D盤寫一個字符。
public static void copy_1()throws IOException
{
//創建目的地。
FileWriter fw = new FileWriter( "RuntimeDemo_copy.txt" );
//與已有文件關聯。
FileReader fr = new FileReader( "RuntimeDemo.java" );
int ch = 0 ;
while ((ch=fr.read())!=- 1 )
{
fw.write(ch);
}
fw.close();
fr.close();
} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | /* 複製一個圖片 思路: 1,用字節讀取流對象和圖片關聯。 2,用字節寫入流對象創建一個圖片文件。用於存儲獲取到的圖片數據。 3,通過循環讀寫,完成數據的存儲。 4,關閉資源。 */ import java.io.*; class CopyPic {
public static void main( String [] args)
{
FileOutputStream fos = null ;
FileInputStream fis = null ;
try
{
fos = new FileOutputStream( "c:\\2.bmp" );
fis = new FileInputStream( "c:\\1.bmp" );
byte[] buf = new byte[ 1024 ];
int len = 0 ;
while ((len=fis.read(buf))!=- 1 )
{
fos.write(buf, 0 ,len);
}
}
catch (IOException e)
{
throw new RuntimeException( "複製文件失敗" );
}
finally
{
try
{
if (fis!= null )
fis.close();
}
catch (IOException e)
{
throw new RuntimeException( "讀取關閉失敗" );
}
try
{
if (fos!= null )
fos.close();
}
catch (IOException e)
{
throw new RuntimeException( "寫入關閉失敗" );
}
}
} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.io.*; class TransStreamDemo {
public static void main( String [] args) throws IOException
{
//獲取鍵盤錄入對象。
//InputStream in = System.in;
//將字節流對象轉成字符流對象,使用轉換流。InputStreamReader
//InputStreamReader isr = new InputStreamReader(in);
//爲了提高效率,將字符串進行緩衝區技術高效操作。使用BufferedReader
//BufferedReader bufr = new BufferedReader(isr);
//鍵盤的最常見寫法。
BufferedReader bufr =
new BufferedReader( new InputStreamReader(System. in ));
// OutputStream out = System.out; // OutputStreamWriter osw = new OutputStreamWriter(out); // BufferedWriter bufw = new BufferedWriter(osw);
BufferedWriter bufw = new BufferedWriter( new OutputStreamWriter(System.out));
String line = null ;
while ((line=bufr.readLine())!= null )
{
if ( "over" .equals(line))
break ;
bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}
bufr.close();
} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import java.io.*; import java.util.*; class PropertiesDemo {
public static void main( String [] args) throws IOException
{
//method_1();
loadDemo();
}
public static void loadDemo()throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream( "info.txt" );
//將流中的數據加載進集合。
prop.load(fis);
prop.setProperty( "wangwu" , "39" );
FileOutputStream fos = new FileOutputStream( "info.txt" );
prop.store(fos, "haha" );
// System.out.println(prop);
prop.list(System.out);
fos.close();
fis.close();
}
//演示,如何將流中的數據存儲到集合中。
//想要將info.txt中鍵值數據存到集合中進行操作。
/*
1,用一個流和info.txt文件關聯。
2,讀取一行數據,將該行數據用"="進行切割。
3,等號左邊作爲鍵,右邊作爲值。存入到Properties集合中即可。
*/
public static void method_1()throws IOException
{
BufferedReader bufr = new BufferedReader( new FileReader( "info.txt" ));
String line = null ;
Properties prop = new Properties();
while ((line=bufr.readLine())!= null )
{
String [] arr = line.split( "=" );
///System.out.println(arr[0]+"...."+arr[1]);
prop.setProperty(arr[ 0 ],arr[ 1 ]);
}
bufr.close();
System.out.println(prop);
} // 設置和獲取元素。
public static void setAndGet()
{
Properties prop = new Properties();
prop.setProperty( "zhangsan" , "30" );
prop.setProperty( "lisi" , "39" ); // System.out.println(prop);
String value = prop.getProperty( "lisi" );
//System.out.println(value);
prop.setProperty( "lisi" , 89 + "" );
Set< String > names = prop.stringPropertyNames();
for ( String s : names)
{
System.out.println(s+ ":" +prop.getProperty(s));
}
}
} |