Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308071Chapter9StringsandTextI/OLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308072MotivationsOftenyouencountertheproblemsthatinvolvestringprocessingandfileinputandoutput.Supposeyouneedtowriteaprogramtoreplacealloccurrencesofawordwithanewwordinafile.Howdoyousolvethisproblem?Thischapterintroducesstringsandtextfiles,whichwillenableyoutosolvethisproblem.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308073ObjectivesTousetheStringclasstoprocessfixedstrings(§9.2).TousetheCharacterclasstoprocessasinglecharacter(§9.3).TousetheStringBuilder/StringBufferclasstoprocessflexiblestrings(§9.4).TodistinguishamongtheString,StringBuilder,andStringBufferclasses(§9.2-9.4).Tolearnhowtopassargumentstothemainmethodfromthecommandline(§9.5).TodiscoverfilepropertiesandtodeleteandrenamefilesusingtheFileclass(§9.6).TowritedatatoafileusingthePrintWriterclass(§9.7.1).ToreaddatafromafileusingtheScannerclass(§9.7.2).(GUI)Toopenfilesusingadialogbox(§9.8).Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308074TheStringClassConstructingaString:–Stringmessage=WelcometoJava“;–Stringmessage=newString(WelcometoJava“);–Strings=newString();ObtainingStringlengthandRetrievingIndividualCharactersinastringStringConcatenation(concat)Substrings(substring(index),substring(start,end))Comparisons(equals,compareTo)StringConversionsFindingaCharacteroraSubstringinaStringConversionsbetweenStringsandArraysConvertingCharactersandNumericValuestoStringsLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308075ConstructingStringsStringnewString=newString(stringLiteral);Stringmessage=newString(WelcometoJava);Sincestringsareusedfrequently,Javaprovidesashorthandinitializerforcreatingastring:Stringmessage=WelcometoJava;Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308076StringsAreImmutableAStringobjectisimmutable;itscontentscannotbechanged.Doesthefollowingcodechangethecontentsofthestring?Strings=Java;s=HTML;Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308077TraceCodeStrings=Java;s=HTML;:StringStringobjectforJavasAfterexecutingStrings=Java;Afterexecutings=HTML;:StringStringobjectforJava:StringStringobjectforHTMLContentscannotbechangedThisstringobjectisnowunreferencedsanimationLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308078TraceCodeStrings=Java;s=HTML;:StringStringobjectforJavasAfterexecutingStrings=Java;Afterexecutings=HTML;:StringStringobjectforJava:StringStringobjectforHTMLContentscannotbechangedThisstringobjectisnowunreferencedsanimationLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308079InternedStringsSincestringsareimmutableandarefrequentlyused,toimproveefficiencyandsavememory,theJVMusesauniqueinstanceforstringliteralswiththesamecharactersequence.Suchaninstanceiscalledinterned.Forexample,thefollowingstatements:Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.013213080710Examplesdisplays1==sisfalses1==s3istrueAnewobjectiscreatedifyouusethenewoperator.Ifyouusethestringinitializer,nonewobjectiscreatediftheinternedobjectisalreadycreated.Strings1=WelcometoJava;Strings2=newString(WelcometoJava);Strings3=WelcometoJava;System.out.println(s1==s2is+(s1==s2));System.out.println(s1==s3is+(s1==s3));:StringInternedstringobjectforWelcometoJava:StringAstringobjectforWelcometoJavas1s2s3Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.013213080711TraceCodeStrings1=WelcometoJava;Strings2=newString(WelcometoJava);Strings3=WelcometoJava;:StringInternedstringobjectforWelcometoJavas1animationLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.013213080712TraceCodeStrings1=WelcometoJava;Strings2=newString(WelcometoJava);Strings3=WelcometoJava;:StringInternedstringobjectforWelcometoJava:StringAstringobjectforWelcometoJavas1s2Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.013213080713TraceCodeStrings1=WelcometoJava;Strings2=newString(WelcometoJava);Strings3=WelcometoJava;:StringInternedstringobjectforWelcometoJava:StringAstringobjectforWelcometoJavas1s2s3Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.013213080714StringComparisonsjava.lang.String+equals(s1:String):boolean+equalsIgnoreCase(s1:String):boolean+compareTo(s1:String):int+compareToIgnoreCase(s1:String):int+regionMatches(toffset:int,s1:String,offset:int,len:int):boolean+regionMatches(ignoreCase:boolean,toffset:int,s1:String,offset:int,len:int):boolean+star