1 object Demo2_SdtIn { 2 def main(args: Array[String]): Unit = { 3 //输入信息 4 println("请输入你的名字:") 5 val name:String = StdIn.readLine() 6 println("输入年龄:") 7 val age: Int = StdIn.readInt() 8 9 //控制台打印输出 10 println(s"年龄${age}的${name}") 11 } 12 13 }
1 object Demo3_FileIO { 2 def main(args: Array[String]): Unit = { 3 //1、从文件中读取数据,然后用foreach打印出来 4 Source.fromFile("D:\\IDEA\\Test\\sc\\src\\demo3").foreach(print) 5 6 //2、将数据写入文件 7 val writer = new PrintWriter(new File("D:\\IDEA\\Test\\sc\\src\\demo3_output.")) 8 //writer调用write方法 9 writer.write("hello scala!") 10 writer.close() 11 } 12 13 }