DT Big Data Dream Factory, plug in Big Data wings

Source: Internet
Author: User
Tags webfile

2015.7.9

DT Big Data Dream Factory Scala No such good video, as long as every day to see a little, you will have a little harvest, not just the code, but also some philosophy to look at things, through the real scene to think about the code, this is the essence of this video, to learn big data, There is nothing to hesitate, see directly on it, your success is not how much you work hard, but you did a successful thing. Shihttp://pan.baidu.com/s/1ktotmqz

Today, let's talk about the process of file this transmission, I knocked a bit of code to try, this is I through the video, the distribution of some positive energy, absolutely just out of the oven, but also warm.

First, on the code:

Package Com.dt.scala.basic



Import Scala.io.Source

Import Java.io.File

Import Java.io.PrintWriter

Import Java.io.FileWriter

Import Java.io.BufferedWriter

Import Java.util.Scanner



Object Fileops {

def main (args:array[string]): Unit = {

Val file=source.fromfile ("E:\\hehe.txt", "UTF8")

for (line <-file.getlines) {println (line)}

File.close ();

Val webFile = Source.fromurl ("http://spark.apache.org/")

Webfile.foreach (print)

Webfile.close

Val Webfile=source.fromurl ("http://spark.apache.org/")

For (Line<-webfile.getlines ()) {println (line)}

println ("**********************************************************")

Webfile.foreach {print}

Webfile.close ()

Val writer = new PrintWriter (New File ("ScalaFile.txt"))

for (I <-1 to) writer.println (i)

Writer.close ()

Val writer=new printwriter (New File ("ScalaFile.txt"))

for (I<-1 to) writer.println (i)

Writer.close ()

Val filll=new File ("ScalaFile.txt")

if (!filll.exists ()) {

Filll.createnewfile ();

//      }

Val hehe=new FileWriter (filll.getabsolutefile,true)

Val buffer=new bufferedwriter (hehe)

For (I<-1 to 50) {

Buffer.write ("+i")

//    }

Buffer.close ()

Print ("Please enter your input:")

Val line = ReadLine

println ("Thanks, you just typed:" + line)

Print ("Please enter your input:")

Val Line=readline ()

println ("Thanks,you Just typed:" +line)

Val scan = new Scanner (system.in);

println (Scan.next ())

}

}

The first is the file's acquisition and output file content,

1) If the file is entered in UTF8 format, the source.fromfile should be added to a parameter, that is, "UTF8", if not added by the default is the encoding format to determine the project is UTF8, the file is GBK, if not add a second parameter, It will appear garbled,

2) in the For loop, if the number of cycles is not used, it is best to use foreach () to save the intermediate variables. It is best not to use for (line<-file.getlines). However, if you need to use the number of loops, such as writing 1 through 50 to a file, you should use the for (I<-1 to 50).

3) But with PrintWriter can not append files, if you want to append a text at the end of a file, you have to use FileWriter, the second parameter is true, you can append.

4) Because Scala's readline is an outdated method, it has been drawn, so it is recommended that you use scanner.


DT Big Data Dream Factory, plug in Big Data wings

Related Article

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.