The youngster, who was eight when the first assault took place, eventually gave the book to a trusted friend and begged her not to tell anybody, Preston Crown Court was told.
James Knowles, 52, of Knowsley Close, Hoghton, near Preston, is on trial at Preston Crown Court over two allegations he sexually touched the youngster at a house in Chorley.
The case is expected to conclude on Friday.
The victim, who cannot be identified for legal reasons, is expected to give evidence from behind a screen.
Opening the case, prosecutor David Temkin said: "It's a case about sexual abuse. The prosecution say this defendant sexually abused the girl and that there were two occasions of abuse - the first when she was between eight and nine and the second when she was aged between 12 and 13.
"The first time was when she was eight or nine. "
He added the second time the girl had tried to push the defendant's hand away, for him to reply he was "only being affectionate."
Mr Temkin added: "It was some time before she told anyone about the abuse. You will hear she wrote about it in a book and gave the book to a friend of hers, a school friend. She asked the friend whether she would keep it secret and she agreed she would keep it secret."
The court heard police launched a probe when the girl confided in a relative and a teacher at her school and the book was eventually passed on to officers.
Knowles voluntarily attended a police interview in June 2013 in which he claimed the girl had told lies about him, and suggested one of her relatives was behind the accusations.