Common methods for reading and writing Java files

Source: Internet
Author: User

TODO auto-generated Method Stub
File File = new file ("D:\\android\\workspace\\practice1\\src\\test4", "new1.txt");
String str = file.getname ();
File.delete ();

try{
Runtime RT = Runtime.getruntime ();
//
File File = new file ("D:\\Program Files (x86) \\CodeBlocks", "Codeblocks.exe");
System.out.println ("Open codeblocks");
Rt.exec (File.getabsolutepath ());
File = new file ("d:\\ Cool dog \\KGMusic", "KuGou.exe");
System.out.println ("Open the Cool Dog");
Rt.exec (File.getabsolutepath ());
File = new File ("d:\\ net Master \\ADSafe", "ADSafeSvc.exe");
SYSTEM.OUT.PRINTLN ("Open Net Master");
Rt.exec (File.getabsolutepath ());
}catch (Exception e) {
System.out.println (e);
//}


try{
File F = new file ("D:\\android\\workspace\\practice1\\src\\test4", "file.txt");
FileInputStream in = new FileInputStream (f);
int n =-1;
byte [] a= new byte[100];
//
while ((n = in.read (A, 0, 100))! =-1) {
string s = new String (A, 0, N);
System.out.print (s);
//}
In.close ();
//}
catch (Exception e) {
System.out.println (e);
//}

try{
byte []b = "I am your Father". GetBytes ();
File File = new file ("D:\\android\\workspace\\practice1\\src\\test4", "empty.txt");
FileOutputStream out = new FileOutputStream (file);
Out.write (b);
Out.close ();
out = new FileOutputStream (file, true);
b = "The text can be appended to the end of the file when the file character output stream has a true parameter in the constructor method." GetBytes ();
Out.write (b);
//
Out.close ();
//
//}
catch (Exception e) {
System.out.println (e);
//}

byte []b = "I am your uncle". GetBytes ();
System.out.println (b);

File SourceFile = new file ("D:\\android\\workspace\\practice1\\src\\test4", "a.txt");
File TargetFile = new file ("D:\\android\\workspace\\practice1\\src\\test4", "b.txt");
//
Char c[] = new CHAR[19];
try{
Reader in = new FileReader (sourcefile);
Writer out = new FileWriter (targetfile, true);
int n =-1;
while ((n = in.read (c))! =-1) {
Out.write (c, 0, N);
//}
Out.flush ();//causes the contents of the buffer to be written to the destination immediately
Out.close ();
}catch (Exception e) {
System.out.println (e);
//
//}

try{
//
FileReader Inone = new FileReader ("D:\\android\\workspace\\practice1\\src\\test4\\a.txt");
BufferedReader bufferedread = new BufferedReader (inone);
//
FileWriter tofile = new FileWriter ("D:\\android\\workspace\\practice1\\src\\test4\\to.txt");
BufferedWriter bufferedwrite = new BufferedWriter (tofile);
//
String str = NULL;
//
while ((str = bufferedread.readline ()) = null) {
StringTokenizer Fenxi = new StringTokenizer (str);
str = str + "Number of words:" + fenxi.counttokens ();
Bufferedwrite.write (str);
Bufferedwrite.newline ();
//}
//
Bufferedwrite.close ();
Tofile.close ();
Bufferedread.close ();
Inone.close ();
//
//
//}
catch (Exception e) {
System.out.println (e);
//}

//
Randomaccessfile inandout = null;
int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 111};
try{
Inandout = new Randomaccessfile ("D:\\android\\workspace\\practice1\\src\\test4\\random.txt", "RW");
for (int i = 0; i < data.length; i++) {
Inandout.writeint (Data[i]);
//}
//
for (Long i = data.length-1; I >= 0; i--) {
Inandout.seek (i * 4);
System.out.printf ("\t%d", Inandout.readint ());
//}
Inandout.close ();
}catch (Exception e) {
System.out.println (e);
//}
//

System.out.println (Math.Round (-13.4));
char bad = ' bad ';

Ok:while (True) {
while (true) {
System.out.println ("I Only cycle once");
Break OK;
}
}

int num = (int) math.random () * 100;


Break String s = null;
char good = bad;

}

Common methods for reading and writing Java files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.