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