Objective-C:
-(void)create{ for (int i=0; i<20;i++) { NSString *fp = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%i.txt",i]]; NSString *ss= @""; for(int i=0;i<100;i++){ int value = arc4random() % 10; NSString *svalue=[NSString stringWithFormat:@"%i",value]; ss=[ss stringByAppendingString:svalue]; } [ss writeToFile:fp atomically:YES encoding:NSUTF8StringEncoding error:nil]; } NSLog(@"生成成功"); }
C++:
#include <iostream> #include <cstdlib> #include <fstream> #include <ctime> #include <string> #include <sstream> using namespace std; int main(int argc, const char * argv[]) { //产生20个100B文件 srand((unsigned)time(NULL)); for (int j=0; j<20;j++ ) { //定义文件路径,生成不同文件名的文件 //C++String的动态性能真是差。。 string a="/Users/way/Desktop/test/yes[",b,c="].txt"; stringstream ss; ss<<j; ss>>b; string name=a+b+c; ofstream of(name); //随机产生100个数字写入文件 for (int i=0; i<100; i++) { of<<rand()%10; } of.close(); } cout<<"生成成功"<<endl; return 0; }
Java:
import java.io.*; public class Main { public static void main(String[] args){ try{ for (int j=0; j<20;j++ ) { //定义文件输出流 String a="/Users/way/Desktop/test2/yes[",b,c="].txt"; b=Integer.toString(j); String nameString=a+b+c; FileWriter wf=new FileWriter(nameString); //定义字符输出流 BufferedWriter bw=new BufferedWriter(wf); String ss =""; for (int i=0;i<100;i++){ int num=(int) (Math.random()*10); ss=ss+Integer.toString(num); } bw.write(ss); bw.close(); wf.close(); System.out.println(ss); } System.out.println("生成成功"); }catch(IOException ie){ System.out.println(ie); }catch(Exception e){ System.out.println(e); } } }
时间: 2024-10-15 01:13:50